Once Upon a Time in Formats...!

Once Upon a Time in Formats...!

So, this just happened:

There was a JSON who wanted to be a CSV, he had so many paths for it. But he wanted to transform himself into CSV in a snap of a finger. Tricky huh! Let us see the whole journey of JSON, what he went through to get himself converted into CSV.

Just like each one of us thinks first of a shortcut. JSON also thought google has everything, why not use a simple online tool converter and get the desired result in seconds. But there he stopped and thought "Am I safe?" Ah! obviously not, using online tools means getting your privacy invaded, data leaked and many other major concerns.

Moving on, JSON met other guys en route like Python, Go, Java, C# who were ready to help JSON, but he founded this way to be really tiring, exhaustive and yes cliche! Meanwhile, he was getting really impatient.

He kept on moving,

Then he remembered his friend " jq ", he straightaway went to jq and told his situation. He felt so relieved when jq said "Oh! Catching leaves in a basket? Dude, I catch the whole tree ;)"

jq is a lightweight and flexible command-line JSON processor. If you are a CLI freak, consider it like sed for JSON data – use it to slice and filter transform the JSON in desired form.

Let us see how jq helped JSON -

JSON’s body structure:

[
  {
      "id": 1,
      "name": "Apple",
      "price": 100
  },
  {
      "id": 2,
      "name": "Orange",
      "price": 65
  }
]

CSV's body he intended to achieve:

1,"Apple",100
2,"Orange",65

The knowledge imparted by jq: (only valid in the universe of Linux / Unix)

cat fruit.json | jq -r '.[] | [.id, .name, .price] | @csv'

So,

jq has lots of filters, we can specify what filters we want to use and connect them through the pipe symbol ‘|’ to create a new filter.

Here,

  • ' . ' represents everything that is present in the JSON.
cat fruit.json | jq . 
  • For eg., if we use the above command, we will get the unmodified JSON in the output as it is. But, when ' . ' is clubbed with any of the field names in JSON it returns only the values present in that particular field.
  • Using the filter .[] we can extract the values for all fields in an object.
  • @csv, an operator that formats the output as CSV. Also@csv assumes values like “Apple” as strings including the quotes. The quotes are then escaped and extra quotes are added so it becomes as \"Apple\". That is why, we add the flag -r to jq which drops one set of quotes.

By now, I hope you are able to interpret what I tried to convey, I won't go in deep cause it's just the start, dive in and try to measure the depth yourself. You may also use https://jqplay.org/

MORAL OF THE STORY:
Go get your hands dirty and experiment cause there is a lot more outside than just searching for shortcuts.


Reza Amin

Cloud Engineering Manager

5 年

Concise and helpful, thank you!

要查看或添加评论,请登录

Kopal Gupta的更多文章

  • Helm to the rescue!

    Helm to the rescue!

    Hey all! In this blog, all I am going to do is talk about the basic concepts and need for Helm. What is Helm? According…

    16 条评论
  • Tired of running apps manually? Let's do Systemctl.

    Tired of running apps manually? Let's do Systemctl.

    So, it happened to me. I was trying to deploy a Java Spring boot angular app on an Amazon EC2 Ubuntu machine.

    16 条评论
  • HTTP codes? Looks Easy, but ain’t.

    HTTP codes? Looks Easy, but ain’t.

    Working with application or web development means interacting with HTTP responses. So, what actually are HTTP codes?…

    4 条评论

社区洞察

其他会员也浏览了