I get several jsons which I have to transform and join, what I do with pandas and afterwards I have to generate a json as well.
the structure of the final json is fixed.
sometimes some fields in the jsons are missing (which is correct) but I have to keep the fields in the joined object wich also works fine.
I have to convert all NaN values to None to get afterwards a valid json with null values but after the groupby operation it converts some None values back to NaN.
See the example attached:
import pandas as pd
import json
dict1 = {
"items": [
{
"name": "Project1",
"projectId": "1",
},
{
"name": "Project2",
"projectId": "2",
},
{
"name": "Project3",
"projectId": "3",
}
]
}
dict2 = {
"items": [
{
"attr1": "ABC",
"attr2": "DEF1",
"attr3": "GHI1",
"projectId": "1",
"services":[
{
"sname": "Service1",
},
{
"sname": "Service2",
}
]
},
{
"attr1": "ABC",
"attr2": "DEF2",
"attr3": "GHI2",
"projectId": "2",
"services":[
{
"sname": "Service1",
},
{
"sname": "Service2",
}
]
}
]
}
dict_head = {
"id":"some-guid",
"name":"some name",
"content" :[
]
}
df1 = pd.DataFrame(dict1["items"])
# df2 = pd.DataFrame(dict2["items"])
df2 = pd.json_normalize(
data = dict2['items'],
record_path = ['services'],
meta = [
'projectId',
'attr1',
'attr2',
'attr3'
]
)
df_joined = df1.set_index("projectId").join(df2.set_index("projectId"))
print("df_joined_1")
print(df_joined)
#convert all NaN vales to None whichs works well
df_joined= df_joined.where(pd.notnull(df_joined), None)
print("df_joined_2")
print(df_joined)
df_grouped = df_joined.groupby(['projectId','name','attr1','attr2','attr3'], dropna=False)['sname'].apply(list).reset_index().to_dict(orient='records')
#suddenly the None values of the grouped fields are conveted back to NaN???
print("df_grouped:")
print(df_grouped)
dict_head["content"] = df_grouped
print("dict_head:")
print(dict_head)
print("dict_head as json:")
print(json.dumps(dict_head, indent=3))
output of Project 3 where you see both NaN an null, I would expect that all NaN are null values
{
"projectId": "3",
"name": "Project3",
"attr1": NaN,
"attr2": NaN,
"attr3": NaN,
"sname": [
null
]
}
Fobber is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.