0

I have two JSON strings as sample:

json_1 = [
    {
        "breadth": 48.04,
        "vessel_id": 1, 
        "vessel_name": "SHIP-01", 
        "vessel_type": "Crude Oil Tanker", 
        "year_built": 2012
     },
     {
        "breadth": 42,
        "vessel_id": 2,
        "vessel_name": "SHIP-02",
        "vessel_type": "Crude Oil Tanker",
        "year_built": 2016
     }
]

json_2 = [
    {
        "Ballast_miles": 43575.8,
        "Ballast_miles_pct": 36.1,
        "org_id": 1,
        "port_days": 383.5,
        "sea_days": 414.9,
        "total_days": 798.4,
        "vessel_id": 1
    },
    {
        "Ballast_miles": 21642.7,
        "Ballast_miles_pct": 29.8,
        "org_id": 1, 
        "port_days": 325.7,
        "sea_days": 259.8,
        "total_days": 585.5,
        "vessel_id": 2
    }
] 

I want to combine these two JSON based on vessel_id.

My output format should look like:

[{ vesselId: 1,
 json1:{},
 json2:{}
},
{ vesselId: 2,
 json1:{},
 json2:{}
}]

What I've tried so far is:

data = {'First_Json': json_1, 'Second_Json': json_2}
json.dumps(data)

But this combines entirely without checking based on vessel_id.

2
  • could you please share what you've tried so far ? Commented Nov 27, 2019 at 10:50
  • data = { 'First_Json' : json_1, 'Second_Json' : json_2 } json.dumps(data) This combines entirely without checking based on vessel_id Commented Nov 27, 2019 at 10:51

1 Answer 1

2

Something like this?

json_1 = [{ "breadth": 48.04, "vessel_id": 1, "vessel_name": "SHIP-01", "vessel_type": "Crude Oil Tanker", "year_built": 2012 }, { "breadth": 42,  "vessel_id": 2, "vessel_name": "SHIP-02", "vessel_type": "Crude Oil Tanker", "year_built": 2016 }]
json_2 = [{ "Ballast_miles": 43575.8, "Ballast_miles_pct": 36.1, "org_id": 1, "port_days": 383.5, "sea_days": 414.9, "total_days": 798.4, "vessel_id": 1 }, { "Ballast_miles": 21642.7, "Ballast_miles_pct": 29.8,  "org_id": 1, "port_days": 325.7, "sea_days": 259.8, "total_days": 585.5, "vessel_id": 2 }] 

from collections import defaultdict
result = defaultdict(dict)
for item in json_1:
    result[item['vessel_id']]['json_1'] = item
for item in json_2:
    result[item['vessel_id']]['json_2'] = item

[{"vessel_id" : k,
  "json1" : v['json_1'],
  "json2" : v['json_2']}
    for k,v in result.items()]

Output:

[{'json1': {'breadth': 48.04,
   'vessel_id': 1,
   'vessel_name': 'SHIP-01',
   'vessel_type': 'Crude Oil Tanker',
   'year_built': 2012},
  'json2': {'Ballast_miles': 43575.8,
   'Ballast_miles_pct': 36.1,
   'org_id': 1,
   'port_days': 383.5,
   'sea_days': 414.9,
   'total_days': 798.4,
   'vessel_id': 1},
  'vessel_id': 1},
 {'json1': {'breadth': 42,
   'vessel_id': 2,
   'vessel_name': 'SHIP-02',
   'vessel_type': 'Crude Oil Tanker',
   'year_built': 2016},
  'json2': {'Ballast_miles': 21642.7,
   'Ballast_miles_pct': 29.8,
   'org_id': 1,
   'port_days': 325.7,
   'sea_days': 259.8,
   'total_days': 585.5,
   'vessel_id': 2},
  'vessel_id': 2}]

If you want to remove the redundant vessel_id, try using for loop with a del command on each dict

Sign up to request clarification or add additional context in comments.

4 Comments

Can we Keep when vessel id is 1 then in one structure { json 1 and 2 } like that ?
[{ vesselId: 1, json1:{}, json2:{} }, { vesselId: 2, json1:{}, json2:{} }] Something like above output structure !
Is it possible to add vessel_name to the json also ?
yes, use [{"vessel_id" : k, "vessel_name" : v['json_1']['vessel_name'], "json1" : v['json_1'], "json2" : v['json_2']} for k,v in result.items()]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.