0

I just study json in python. Let's say I have a data like:

{
   "id":1,
   "class":"c1",
   "owner":"myself",
   "metadata":{
      "m1":{
         "value":"m1_1",
         "timestamp":"d1"
      },
      "m2":{
         "value":"m1_2",
         "timestamp":"d2"
      },
      "m3":{
         "value":"m1_3",
         "timestamp":"d3"
      },
      "m4":{
         "value":"m1_4",
         "timestamp":"d4"
      }
   },
   "a1":{
      "a11":[

      ]
   },
   "m1":{

   },
   "comm1":"COMM1",
   "comm2":"COMM21529089656387",
   "share":"xxx",
   "share1":"yyy",
   "hub1":"h1",
   "hub2":"h2",
   "context":[

   ]
}

I am wondering how I can convert such json to csv when it includes multiple nests.

I hope the csv structure can include all the fields, such as:

id   class   owner   metadata-m1-value   metadata-m2-value ......
 1    c1      myself      m1_1               m1_2......

I do not understand json very well. so maybe my example is not very good. Anyway, showing all the fields in csv. If you think there is a better way to do this, please use your way.

I searched Pandas read nested json. But json is really new to me and I cannot get the point.

halfer
  • 19,824
  • 17
  • 99
  • 186
Feng Chen
  • 2,139
  • 4
  • 33
  • 62
  • Do you want to convert for this JSON structure or do you need a generic solution? – Waqas Javed Oct 10 '19 at 05:26
  • hi, i want to this one to be csv. of course, a generic solution would be better. I have other json files with slightly different structures – Feng Chen Oct 10 '19 at 05:27
  • please provide the CSV sample you want against this structure. – Waqas Javed Oct 10 '19 at 05:34
  • I changed the post, is it clear now? – Feng Chen Oct 10 '19 at 05:39
  • 1
    This gets asked a lot, try the answers listed in https://stackoverflow.com/questions/20424473/parsing-nested-json-and-writing-it-to-csv – mmenschig Oct 10 '19 at 05:43
  • You need to determine how to "flatten" the json structure into multiple rows of data with all the same fields, as csv doesn't support nesting. – martineau Oct 10 '19 at 05:56
  • Here is a better solution using recursion to flatten the JSON, regardless of how deep the nesting is. [How to flatten a nested JSON from the NASA Weather Insight API in Python](https://stackoverflow.com/questions/58295806/how-to-flatten-a-nested-json-from-the-nasa-weather-insight-api-in-python/58295889#58295889). Once it's in a dataframe, just `df.to_csv('data.csv', index=False)` – Trenton McKinney Oct 17 '19 at 22:37
  • @Trenton: The accepted answer to the duplicate question illustrates how one can flatten a nested JSON data-structure and create CSV records/rows from the results of that, and a similar overall approach would work here, as well. – martineau Oct 17 '19 at 23:51
  • @FengChen: When you have multiple nested objects, you will need to first gather them all up, so they can all be output simultaneously as a single row of the csv file. See if you can do that based on what is shown in the duplicate question's accepted answer. If you get stuck, as a new question and show how you're attempt at doing it yourself. – martineau Oct 18 '19 at 00:02
  • This will answer your question too [How to use flatten_json to recursively flatten deeply nested JSON objects?](https://stackoverflow.com/questions/58442723/how-to-use-flatten-json-to-recursively-flatten-deeply-nested-json-objects) – Trenton McKinney Oct 18 '19 at 02:02

0 Answers0