I have a nested json and would like to convert it to a pandas dataframe using the json_normalize function.
JSON
json_input = [{'measurements': [{'value': 111, 'timestamp': 1},
{'value': 222, 'timestamp': 2}],
'sensor': {'name': 'testsensor',
'id': 1}},
{'measurements': [{'value': 333, 'timestamp': 1},
{'value': 444, 'timestamp': 2}],
'sensor': None},
]
Normalizing
df = pd.json_normalize(json_input, record_path=['measurements'],
meta=['sensor'])
The metadata does not get normalized in the output of the above code:
| | value | timestamp | sensor |
|---|-------|-----------|---------------------------------|
| 0 | 111 | 1 | {'name': 'testsensor', 'id': 1} |
| 1 | 222 | 2 | {'name': 'testsensor', 'id': 1} |
| 2 | 111 | 1 | None |
| 3 | 222 | 2 | None |
Is there a possibility to get the desired output:
| | value | timestamp | sensor.name | sensor.id |
|---|-------|-----------|--------------|-----------|
| 0 | 111 | 1 | 'testsensor' | 1 |
| 1 | 222 | 2 | 'testsensor' | 1 |
| 2 | 111 | 1 | None | None |
| 3 | 222 | 2 | None | None |