skip to Main Content

I have two json datas as

json_1 = [{'purchasedPerson__id': 2, 'credit': 3000}, {'purchasedPerson__id': 4, 'credit': 5000}]
json_2 = [{'purchasedPerson__id': 1, 'debit': 8526}, {'purchasedPerson__id': 4, 'debit': 2000}]

i want to merge both the json and needed optput as

json_final = [{'purchasedPerson__id': 2, 'credit': 3000 , 'debit'=0}, 
{'purchasedPerson__id': 4, 'credit': 5000 , 'debit'=2000},
 {'purchasedPerson__id': 1, 'credit'=0, 'debit': 8526}]

how the above method can be done

3

Answers


  1. # Initialize the final JSON array
    json_final = []
    
    # Loop through the first JSON data set
    for item in json_1:
        # Initialize the final JSON object for this item
        final_item = {'purchasedPerson__id': item['purchasedPerson__id'], 'credit': item['credit'], 'debit': 0}
        # Loop through the second JSON data set
        for item2 in json_2:
            # If the id matches, update the final item with the debit value
            if item['purchasedPerson__id'] == item2['purchasedPerson__id']:
                final_item['debit'] = item2['debit']
        # Add the final item to the final JSON array
        json_final.append(final_item)
    
    # Loop through the second JSON data set
    for item in json_2:
        # Initialize a flag to keep track of whether the item already exists in the final JSON array
        exists = False
        # Loop through the final JSON array
        for final_item in json_final:
            # If the id matches, set the exists flag to True
            if final_item['purchasedPerson__id'] == item['purchasedPerson__id']:
                exists = True
        # If the item does not exist in the final JSON array, add it with credit and debit values of 0
        if not exists:
            json_final.append({'purchasedPerson__id': item['purchasedPerson__id'], 'credit': 0, 'debit': item['debit']})
    
    Login or Signup to reply.
  2. This is a case where pandascan be very convenient. By converting to dataframes and merging on "purchasedPerson__id", you will get the desired output:

    import pandas as pd
    
    json_1 = [{'purchasedPerson__id': 2, 'credit': 3000}, {'purchasedPerson__id': 4, 'credit': 5000}]
    json_2 = [{'purchasedPerson__id': 1, 'debit': 8526}, {'purchasedPerson__id': 4, 'debit': 2000}]
    df1 = pd.DataFrame(json_1)
    df2 = pd.DataFrame(json_2)
    
    df_out = pd.merge(df1, df2, on="purchasedPerson__id", how="outer").fillna(0)
    df_out.to_dict(orient="records")
    

    Output:

    [{'purchasedPerson__id': 2, 'credit': 3000.0, 'debit': 0.0}, {'purchasedPerson__id': 4, 'credit': 5000.0, 'debit': 2000.0}, {'purchasedPerson__id': 1, 'credit': 0.0, 'debit': 8526.0}]
    
    Login or Signup to reply.
  3. json_1 = [{'purchasedPerson__id': 2, 'credit': 3000}, {'purchasedPerson__id': 4, 'credit': 5000}]
    json_2 = [{'purchasedPerson__id': 1, 'debit': 8526}, {'purchasedPerson__id': 4, 'debit': 2000}]
    
    # create a dictionary for the merged data
    data = {}
    
    # loop through each JSON and add the data to the dictionary
    for j in json_1:
        data[j['purchasedPerson__id']] = {'credit': j['credit'], 'debit': 0}
    
    for j in json_2:
        if j['purchasedPerson__id'] in data:
            data[j['purchasedPerson__id']] = {'credit': data[j['purchasedPerson__id']]['credit'], 'debit': j['debit']}
        else:
            data[j['purchasedPerson__id']] = {'credit': 0, 'debit': j['debit']}
    
    # convert the dictionary to a list
    json_final = []
    for key, value in data.items():
        json_final.append({'purchasedPerson__id': key, 'credit': value['credit'], 'debit': value['debit']})
    
    print(json_final)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search