python - how to write the json with subdocument column names to json - Stack Overflow

I have code as below to write a json from the below dictionaryLooks like there is issue in my group by

I have code as below to write a json from the below dictionary Looks like there is issue in my group by or aggregate. It is not generating the JSON as expected as give below.

import json

data = { "some_id": "123456",
 "some_email": "[email protected]",
 "some_number" : 123456}
df = pd.DataFrame(data, index=[0])
#print(df)


for idx, row in df.iterrows():
  # convert each record to a dictionary
  record_dict = row.to_dict()
  json_data = json.dumps(record_dict, indent=1)
  #print(json_data)
  
phone_number = { "some_number" : 123456, "Contact" :{"phone_number": 45464464646,"contact?": "Y"}}
df_phonnumber = pd.DataFrame(phone_number)
#print(df_phonnumber)

merged_df = pd.merge(df, df_phonnumber, left_on='some_number', right_on='some_number', how='left')
#print(merged_df)

#single columns def_size and dep_name
d = (merged_df.groupby(['some_number','some_email','some_id']).apply(lambda x: x[['Contact']]
      .to_dict('r'))
      .reset_index(name='dont_contact'))

json_str = d.to_json(orient='records')

pretty_json = json.dumps(json.loads(json_str), indent=4)
print(pretty_json)

Expecting the JSON as below

{
        "some_number": 123456,
        "some_email": "[email protected]",
        "some_id": "123456",
        "dont_contact": [
            "Phone_Number": "45464464646",
            "Contact?": "Y"
            ]
}

But I am getting the Json as below. What is missing?

{
        "some_number": 123456,
        "some_email": "[email protected]",
        "some_id": "123456",
        "dont_contact": [
            {
                "Contact": "Y"
            },
            {
                "Contact": 45464464646
            }
        ]
    }

can some please help on this to get the json in the above format?

I have code as below to write a json from the below dictionary Looks like there is issue in my group by or aggregate. It is not generating the JSON as expected as give below.

import json

data = { "some_id": "123456",
 "some_email": "[email protected]",
 "some_number" : 123456}
df = pd.DataFrame(data, index=[0])
#print(df)


for idx, row in df.iterrows():
  # convert each record to a dictionary
  record_dict = row.to_dict()
  json_data = json.dumps(record_dict, indent=1)
  #print(json_data)
  
phone_number = { "some_number" : 123456, "Contact" :{"phone_number": 45464464646,"contact?": "Y"}}
df_phonnumber = pd.DataFrame(phone_number)
#print(df_phonnumber)

merged_df = pd.merge(df, df_phonnumber, left_on='some_number', right_on='some_number', how='left')
#print(merged_df)

#single columns def_size and dep_name
d = (merged_df.groupby(['some_number','some_email','some_id']).apply(lambda x: x[['Contact']]
      .to_dict('r'))
      .reset_index(name='dont_contact'))

json_str = d.to_json(orient='records')

pretty_json = json.dumps(json.loads(json_str), indent=4)
print(pretty_json)

Expecting the JSON as below

{
        "some_number": 123456,
        "some_email": "[email protected]",
        "some_id": "123456",
        "dont_contact": [
            "Phone_Number": "45464464646",
            "Contact?": "Y"
            ]
}

But I am getting the Json as below. What is missing?

{
        "some_number": 123456,
        "some_email": "[email protected]",
        "some_id": "123456",
        "dont_contact": [
            {
                "Contact": "Y"
            },
            {
                "Contact": 45464464646
            }
        ]
    }

can some please help on this to get the json in the above format?

Share Improve this question edited Mar 26 at 1:12 Ynjxsjmh 30.1k7 gold badges42 silver badges62 bronze badges asked Mar 25 at 19:50 Virendra WadekarVirendra Wadekar 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Load your nested second dataset via json_normalize() to get easy access to the individual attributes then use apply to construct a new column.

import json
import pandas as pd

data = {
    "some_id": "123456",
    "some_email": "[email protected]",
    "some_number" : 123456
}

phone_number = {
    "some_number" : 123456,
    "Contact" : {"phone_number": 45464464646, "contact?": "Y"}
}

df_data = pd.DataFrame(data, index=[0])
df_phone_number = pd.json_normalize(phone_number)

df_merged = pd.merge(df_data, df_phone_number, left_on='some_number', right_on='some_number', how='left')
df_merged["Contact"] = df_merged.apply(lambda x: {"phone_number": x["Contact.phone_number"], "contact?": x["Contact.contact?"]}, axis=1)
df_merged = df_merged.drop(columns=['Contact.phone_number', 'Contact.contact?'])

json_str = df_merged.to_json(orient='records')
pretty_json = json.dumps(json.loads(json_str), indent=4)
print(pretty_json)

Giving you:

[
    {
        "some_id": "123456",
        "some_email": "[email protected]",
        "some_number": 123456,
        "Contact": {
            "phone_number": 45464464646,
            "contact?": "Y"
        }
    }
]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744172314a4561593.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信