user2920015
user2920015

Reputation: 11

Transform JSON in CSV format to table

I have already extracted the string of JSON data from a url and stored this in a CSV file.

The problem I am facing now is that I would like to represent this data in a table format so that I can upload this to my DB.

An example of my raw JSON data below:

{"pizza":[{"url":"www.pizza.com/1.json","id":1,"via":{"channel":"web","source":{"from":{"[email protected]"},"to":{"[email protected]"},"rel":null}},"created_at":"2013-07-09T08:38:35Z","updated_at":"20132-08-13T09:33:00Z","type":"incident"}]

An example of my CSV formatted JSON:

http://s18.postimg.org/alwadflsp/my_Code.png

An example of my desired output:

http://s18.postimg.org/4md4niqe1/myoutput.png

Would really appreciate help regarding this.

Thank you

Upvotes: 1

Views: 1821

Answers (3)

jq170727
jq170727

Reputation: 14665

Here is a solution using jq. However this part of your data isn't legal JSON

"source":{"from":{"[email protected]"},"to":{"[email protected]"},

Assuming you remove the surrounding {} from the from and to members

"source":{"from":"[email protected]","to":"[email protected]",

then if filter.jq contains the following filter

[
    .pizza[]
  | {url, id} + (.via | {channel} + (.source | {from, to})) + {created_at, updated_at}
]
| (.[0] | keys_unsorted)
, (.[]  | [.[]])
| @csv  

and data.json contains your sample data then the command

$ jq -M -r -f filter.jq data.json

produces

"url","id","channel","from","to","created_at","updated_at"
"www.pizza.com/1.json",1,"web","[email protected]","[email protected]","2013-07-09T08:38:35Z","20132-08-13T09:33:00Z"

Upvotes: 1

ajc
ajc

Reputation: 1725

If your final goal is to save your data obtained from JSON in a database.

My approach would be - 1) Extract the relevant info from the JSON using JSONObject. JSONOBJECT

2) Create a bean with all the info.

3) Pass that bean to the Database class to store the respective values.

Upvotes: 1

miku
miku

Reputation: 188034

There are (numerous) small command line utilities written for that task, maybe you find one of those helpful:

Upvotes: 2

Related Questions