Решение, которое я предлагаю, может быть немного сложным. Представьте, что у вас есть сообщение JSON в строке с именем «msg_str»:
import pandas as pd
msg_str = '{ "messageHeader" : { "messageId" : "4b604b33-7256-47b6-89d6-eb1d92a282e6", "timestamp" : 152520000, "sourceHost" : "test", "sourceLocation" : "test", "tags" : [ ], "version" : "1.0" }, "id_value" : { "id" : "1234", "value" : "333.0" }}'
#first create a dataframe with read_json
p = pd.read_json(msg_str)
# Now you have a dataframe with two columns. Where a column has a value, the other
# has a NaN. Now create a new column only with the values which are not 'NaN'
p['fussion'] = p['id_value'].fillna(p['messageHeader'])
# Delete columns 'id_value' and 'messageHeader' as you don't need them anymore
p = p[['fussion']].reset_index()
# Create a temporal column only to be the index to do a pivot
p['tmp'] = 0
# Do the pivot to convert rows into columns
p = p.pivot(index = 'tmp' ,values='fussion', columns='index')
# Finally get the columns that you are interested in
p = p.reset_index()[['timestamp','id','value']]
print(p)
Результат:
index timestamp id value
0 152520000 1234 333
Затем вы можете добавить этот фрейм данных в фрейм данных, где вы накапливаете свои результаты.
Возможно, есть простейшее решение, но я надеюсь, что оно поможет вам, если бы это было не так.