0

I am trying to convert the below JSON as a dataframe. The below JSON is in a string.

 json_str='{"TileName": "Master",
  "Report Details": 
     [
       {
        "r1name": "Primary",
        "r1link": "link1",
        "report Accessible": ["operations", "Sales"]
       },
       {
       "r2name": "Secondry",
       "r2link": "link2",
       "report Accessible": ["operations", "Sales"]
       }
     ]
  }'

So I am trying to get below df

TileName      ReportAccssible     ReportName     ReportLink
Master        operations           Primary        link1
Master        Sales                Primary        link1
Master        operations           Secondary      link2
Master        Sales                Secondary      link2 

In order to achieve the above, I am trying the below code snippet:

js_str = json.loads(json_str)
df = pd.json_normalize(js_df,'Report Details',['TileName',['report Accessible']],\
record_prefix='Col_',errors='ignore')

But the above code is not giving me the output as per desired format.

What I am missing here?

2
  • Is there wrong in the column ReportLink? I guess you want 'link1' and 'link2' instead of 'r1link' and 'r2link'. Commented Nov 29, 2022 at 9:55
  • yes. Spot on!! I have modified the OP. Commented Nov 29, 2022 at 9:57

1 Answer 1

1

You have wrong record_path, which should be ['Report Details', 'report Accessible'].

js_obj = json.loads(json_str.replace('r2', 'r1'))  # keep columns consistent
df = pd.json_normalize(js_obj, ['Report Details', 'report Accessible'], 
                       ['TileName', ['Report Details', 'r1name'], ['Report Details', 'r1link']])
df.columns = ['ReportAccssible', 'TileName', 'ReportName', 'ReportLink']

You will get what you want.

  ReportAccssible TileName ReportName ReportLink
0      operations   Master    Primary      link1
1           Sales   Master    Primary      link1
2      operations   Master   Secondry      link2
3           Sales   Master   Secondry      link2
Sign up to request clarification or add additional context in comments.

Comments

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.