Shubham
Shubham

Reputation: 81

Convert a json file to csv using shell script without using jq?

I want to convert a json file to csv using shell script without using jq. Is it possible?

Here is a json :

{
  "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
},
{
"id": "0002",
    "type": "donut2",
    "name": "Cake2",
    "ppu": 0.5522,
}

I don't want to use jq.

I want to store it in a csv file.

Upvotes: 7

Views: 19220

Answers (2)

Shawn
Shawn

Reputation: 52344

Bare-bones core-only perl one-liner version, to complement the python and ruby ones already given:

perl -MJSON::PP -0777 -nE '$,=","; say @$_{"id","type","name","ppu"} for @{decode_json $_}' input.json

A more robust one would use a more efficient non-core JSON parser and a CSV module to do things like properly quote fields when needed, but since your sample data doesn't include such fields I didn't bother. Can if requested.


And the unrequested jq version, because that really is the best approach whether you want it or not:

jq -r '.[] | [.id, .type, .name, .ppu] | @csv' input.json

Upvotes: 14

Matias Barrios
Matias Barrios

Reputation: 5056

Bash is not the tool to do this at all. But if for some reason you cannot install jq, you can simply use Python 3, which comes by default in most distros of Linux and in MacOS.

#!/usr/local/bin/python3

import json

objs=json.loads("""
[
    {
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55
    },
    {
        "id": "0002",
        "type": "donut2",
        "name": "Cake2",
        "ppu": 0.5522
    }
]
""")
for item in objs :
    print("{}{}{}{}{}{}{}".format(item['id'],",",item['type'],",",item['name'],",",item['ppu']))

If you do not have Python 3 either, you can then do it in Ruby, which also comes by default in most distros and MacOS :

#!/usr/bin/ruby

require 'json'

content = '
[
    {
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55
    },
    {
        "id": "0002",
        "type": "donut2",
        "name": "Cake2",
        "ppu": 0.5522
    }
]
'

JSON.parse(content).each { |item| puts "#{item['id']},#{item['type']},#{item['name']},#{item['ppu']}" }

You can then redirect the output to a file :

script.rb > output.csv

And thats it.

Nevertheless, if you can be completely sure of the format of your input, you can do some bash magic, specially using awk. But as others also said, please don't do that.

Upvotes: 0

Related Questions