0

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"
   }}]
}
0

2 Answers 2

1

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()}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this works for what I asked. I just updated the question though to wrap it in "response", would you mind updating your answer to make it work for that? I really appreciate it.
Sorry, just fixed
0
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'}

Comments

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.