Mike Deluca
Mike Deluca

Reputation: 1210

Concat character to JSON key name in Python

I have a JSON file like this below and the keys in the custom_fields can vary for each id. I need to import this data into BigQuery but they don't allow field names to begin with a number. So, using Python 3.7, I am trying to find out how can I dynamically concatenate a value to the beginning of those keys within custom_fields without manually specifying each field name?

{
  "response":[{
    "id": "123",
    "custom_fields":{
        "5c30673efc89f7000400001d":"val1",
        "5e34770a8e3d1b010a757981":"val2",
        "5e3477d28e3d1b0140757993":"val3"
   }},
   {
    "id": "456",
    "custom_fields":{
        "5c30673efc89f7000400001d":"val1",
        "5e34770a8e3d1b010a757981":"val2",
        "5e3477d28e3d1b0140757993":"val3"
   }}]
}

The data is coming from an API and saved to cloud storage, with the output being retrieved and formatted to JSON with this:

response = urllib.request.Request('https://www.test.com')
result = urllib.request.urlopen(response)
resulttext = result.read()
jsonResponse = json.loads(resulttext.decode('utf-8'))

Desired output would be like:

{
  "response":[{
    "id": "123",
    "custom_fields":{
        "_5c30673efc89f7000400001d":"val1",
        "_5e34770a8e3d1b010a757981":"val2",
        "_5e3477d28e3d1b0140757993":"val3"
   }},
   {
    "id": "456",
    "custom_fields":{
        "_5c30673efc89f7000400001d":"val1",
        "_5e34770a8e3d1b010a757981":"val2",
        "_5e3477d28e3d1b0140757993":"val3"
   }}]
}

Upvotes: 0

Views: 89

Answers (2)

Vishal Singh
Vishal Singh

Reputation: 6234

If the jsonResponse is like what you've shown in your post then this should do the job fine.

for d in jsonResponse["response"]:
    d["custom_fields"] = {f"_{k}": v for k, v in d["custom_fields"].items()}

Upvotes: 1

learning2learn
learning2learn

Reputation: 411

import pprint

a_dict = {
"id": "123",
"custom_fields":{
    "5c30673efc89f7000400001d":"val1",
    "5e34770a8e3d1b010a757981":"val2",
    "5e3477d28e3d1b0140757993":"val3"
   }
}

print('before')
pprint.pprint(a_dict)

for key in a_dict['custom_fields']:
    k_new = '_' + key
    a_dict['custom_fields'][k_new] = a_dict['custom_fields'].pop(key)

print('after')
pprint.pprint(a_dict)

outputs:

before
{'custom_fields': {'5c30673efc89f7000400001d': 'val1',
                   '5e34770a8e3d1b010a757981': 'val2',
                   '5e3477d28e3d1b0140757993': 'val3'},
 'id': '123'}
after
{'custom_fields': {'_5c30673efc89f7000400001d': 'val1',
                   '_5e34770a8e3d1b010a757981': 'val2',
                   '_5e3477d28e3d1b0140757993': 'val3'},
 'id': '123'}

Upvotes: 0

Related Questions