0

I tried using json_normalize to flatten data but a part of the data is still not flattened. How can I get flattened data for this json. Note: This is a sample json file but the data might increase in the nested part.

Code:

json_file = {
  "data": "abc",
  "data2": 123,
  "results": {
    "name": "w",
    "more_data": [
      {
        "no": "111",
        "code": 3
      }
    ],
    "id": 1
  }
}

data = json_normalize(json_file)
data.to_csv('flatten.csv')

Result:

,data,data2,results.id,results.more_data,results.name
0,abc,123,1,"[{'no': '111', 'code': 3}]",w


results.more_data still gives me a json instead of flattening it.

How can I get it to work?

Expected output:

Since results.more_data is an array even this has to be flattened. Eg:

,data,data2,results.id,results.more_data.0.no,results.more_data.0.code,results.name
    0,abc,123,1, '111', "3",w
2
  • 2
    Do you mind to share with us the expected output? Commented Jun 14, 2018 at 14:06
  • @user32185 Please check the expected output! Commented Jun 14, 2018 at 14:25

1 Answer 1

1

Consider the following demo:

In [105]: json_file = {
     ...:   "data": "abc",
     ...:   "data2": 123,
     ...:   "results": {
     ...:     "name": "w",
     ...:     "more_data": [
     ...:       {
     ...:         "no": "111",
     ...:         "code": 3
     ...:       },
     ...:       {
     ...:         "no": "222",
     ...:         "code": 4
     ...:       }
     ...:
     ...:     ],
     ...:     "id": 1
     ...:   }
     ...: }
     ...:

In [106]:

In [106]: json_normalize(json_file, 
                         [['results','more_data']], 
                         ['data','data2', ['results','id'], ['results','name']],
                         record_prefix='results.more_data.')
Out[106]:
   results.more_data.code results.more_data.no data  data2  results.id results.name
0                       3                  111  abc    123           1            w
1                       4                  222  abc    123           1            w
Sign up to request clarification or add additional context in comments.

1 Comment

Hi MaxU, I'm looking for solution something around this : ,data,data2,results.id,results.more_data.0.no,results.more_data.0.code,results.name 0,abc,123,1, '111', "3",w

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.