0

I know there has been already several questions on this topic, but I can't find the answer.

I have a nested JSON, let's say:

data = {'items': [{'block': 1,
   'log_events': [{'decoded': {'name': 'Transfer'},
     'params': ['param0', 'param1', 'param2']}]},
  {'block': 2,
   'log_events': [{'decoded': {'name': 'SafeReceived'},
     'params': ['param3', 'param4', 'param5']}]}]}

I tried:

pd.json_normalize(
    data=data['items'],
    record_path=['log_events'],
    meta=['block']
)

But I would like to "flatten" both on the 'log_events' and on the 'params'. How can I do that ?

1
  • Please share your expected output. Commented Apr 26, 2022 at 10:45

1 Answer 1

1

IIUC use:

df = pd.json_normalize(
    data=data['items'],
    record_path=['log_events'],
    meta=['block']
).explode('params').reset_index(drop=True)
print (df)
   params  decoded.name block
0  param0      Transfer     1
1  param1      Transfer     1
2  param2      Transfer     1
3  param3  SafeReceived     2
4  param4  SafeReceived     2
5  param5  SafeReceived     2
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.