Home > Software engineering >  create new json data from values in a dictionary and nested dictionary
create new json data from values in a dictionary and nested dictionary

Time:11-09

i have 2 dictionaries and i wish to create a new json data with values from both dictionaries as follows.

dic_a = [{'name': 'puskas',
  'description': 'puskas is the command center for football',
  'size': '251-1K',
  'revenue': '$50M-$100M',
  'industryTags': ['football federation']}]

dic_b = {'page': 1,
 'total': 14,
 'results': [{'id': 'i01',
   'name': {'fullName': 'luka modric',
    'givenName': 'luka',
    'familyName': 'modric'},
    'role': 'leadership',
   'subRole': 'ceo',
   'title': 'CEO',
   'company': {'name': 'puskas'},
   'email': '[email protected]',
   'verified': True},
  {'id': 'i02',
   'name': {'fullName': 'gucci mane',
    'givenName': 'gucci',
    'familyName': 'mane'},
   'role': 'leadership',
   'subRole': 'founder',
   'title': 'Co-founder, CTO',
   'company': {'name': 'puskas'},
   'email': '[email protected]',
   'verified': True},
  {'id': 'i03',
   'name': {'fullName': 'tom ford',
    'givenName': 'tom',
    'familyName': 'ford'},
    'role': 'leadership',
   'subRole': 'founder',
   'title': 'founder',
   'company': {'name': 'puskas'},
   'email': '[email protected]',
   'verified': True}]}

i want to take select values from b, append to a and then convert to json and return as c.

i have tried a few codes off of some syntax i researched here but it don’t work. i am expecting the json result to look like this

json_c = [{'name': 'puskas',
  'description': 'puskas is the command center for football',
  'size': '251-1K',
  'revenue': '$50M-$100M',
  'industryTags': ['football federation'],
  'leads': [{'id': 'i01',
   'name': 'luka modric',
   'title': 'CEO',
   'company': {'name': 'puskas'},
   'email': '[email protected]',
   'verified': True},
  {'id': 'i02',
   'name': 'gucci mane',
   'title': 'Co-founder, CTO',
   'company': {'name': 'gucci'},
   'email': '[email protected]',
   'verified': True},
  {'id': 'i03',
   'name': 'tom ford',
   'title': 'founder',
   'company': {'name': 'xyz'},
   'email': '[email protected]',
   'verified': True}]}]   

CodePudding user response:

such problems can be solved easily with jmespath

import jmespath
import json 

c = dic_a
c[0]['leads'] = jmespath.search('results[].{id:id, name:name.fullName,title:title ,company:company,email:email,verified:verified }',dic_b)

json_string = json.dumps(c, indent=4, ensure_ascii=False)
print(json_string)   

# [
#     {
#         "name": "puskas",
#         "description": "puskas is the command center for football",
#         "size": "251-1K",
#         "revenue": "$50M-$100M",
#         "industryTags": [
#             "football federation"
#         ],
#         "leads": [
#             {
#                 "id": "i01",
#                 "name": "luka modric",
#                 "title": "CEO",
#                 "company": {
#                     "name": "puskas"
#                 },
#                 "email": "[email protected]",
#                 "verified": true
#             },
#             {
#                 "id": "i02",
#                 "name": "gucci mane",
#                 "title": "Co-founder, CTO",
#                 "company": {
#                     "name": "puskas"
#                 },
#                 "email": "[email protected]",
#                 "verified": true
#             },
#             {
#                 "id": "i03",
#                 "name": "tom ford",
#                 "title": "founder",
#                 "company": {
#                     "name": "puskas"
#                 },
#                 "email": "[email protected]",
#                 "verified": true
#             }
#         ]
#     }
# ]
  • Related