rogawiv352
rogawiv352

Reputation: 15

How to convert one schema to another Python

I have 2 schemas.

First schema:

{
   "id": str,
   "fields": {
     "firstName": str,
     "lastName": str,
     "dateOfBirth": str,  # formatting "mm-dd-yyyy"
     "email": str,
     "lifetimeValue": str  # formatting "$xx.xx" 
   }
}

The second:

{
 "first_name": str,
 "last_name": str,
 "birthdate": str,  # formatting "yyyy-mm-dd"
 "email": str,
 "custom_properties": {
   "airtable_id": str,
   "lifetime_value": float
 }
}

I have a variable with people = response.text who need to be converted according to the second scheme. As you can see, I copied people to variable json_data. But my question is: how do i convert just people using the variable instead of explicitly writing the value to json_data?

import requests
import json

response = requests.get('https://challenge-automation-engineer-xij5xxbepq-uc.a.run.app/people/',
                         headers={'Authorization': 'fFz8Z7OpPTSY7gpAFPrWntoMuo07ACjp'})
json_data = [{"id":"rec123456789","fields":{"firstName":" James","lastName":"Larry","dateOfBirth":"31-01-1986","email":"[email protected]","lifetime_value":"$125500.00"}},{"id":"rec987654321","fields":{"firstName":"Jane","lastName":"Doe","dateOfBirth":"31-01-1971","email":"[email protected]","lifetime_value":"$1500.50"}},{"id":"rec132457689","fields":{"firstName":"John","lastName":"Doe","dateOfBirth":"31-01-1973","email":"[email protected]","lifetime_value":"$229.00"}}]

converted_data = []

for data in json_data:
    converted_data.append({
        "first_name": data["fields"]["firstName"].strip(),
        "last_name": data["fields"]["lastName"],
        "birthdate": data["fields"]["dateOfBirth"][-4:] + "-" + data["fields"]["dateOfBirth"][:2] + "-" + data["fields"]["dateOfBirth"][3:5],
        "email": data["fields"]["email"],
        "custom_properties": {
            "airtable_id": data["id"],
            "lifetime_value": float(data["fields"]["lifetime_value"][1:].replace(",", ""))
        }
    })

print(json.dumps(converted_data, indent=4))

Upvotes: 0

Views: 384

Answers (2)

impy
impy

Reputation: 41

If you want to convert the json response to a python dictionary, you can do the following:

json_data = json.loads(response.text)

Upvotes: 1

Nova
Nova

Reputation: 452

Try this code:

import requests
import json

response = requests.get('https://challenge-automation-engineer-xij5xxbepq-uc.a.run.app/people/',
                         headers={'Authorization': 'fFz8Z7OpPTSY7gpAFPrWntoMuo07ACjp'})

json_data = json.loads(response.text)

converted_data = []

for data in json_data:
    converted_data.append({
        "first_name": data["fields"]["firstName"].strip(),
        "last_name": data["fields"]["lastName"],
        "birthdate": data["fields"]["dateOfBirth"][-4:] + "-" + data["fields"]["dateOfBirth"][:2] + "-" + data["fields"]["dateOfBirth"][3:5],
        "email": data["fields"]["email"],
        "custom_properties": {
            "airtable_id": data["id"],
            "lifetime_value": float(data["fields"]["lifetime_value"][1:].replace(",", ""))
        }
    })

print(json.dumps(converted_data, indent=4))

I just replaced the following line to convert response.text to json_data: From:

json_data = [{"id":"rec123456789","fields":{"firstName":" James","lastName":"Larry","dateOfBirth":"31-01-1986","email":"[email protected]","lifetime_value":"$125500.00"}},{"id":"rec987654321","fields":{"firstName":"Jane","lastName":"Doe","dateOfBirth":"31-01-1971","email":"[email protected]","lifetime_value":"$1500.50"}},{"id":"rec132457689","fields":{"firstName":"John","lastName":"Doe","dateOfBirth":"31-01-1973","email":"[email protected]","lifetime_value":"$229.00"}}]

To:

json_data = json.loads(response.text)

Upvotes: 2

Related Questions