Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
 "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":"jlarry@example.co","lifetime_value":"$125500.00"}},{"id":"rec987654321","fields":{"firstName":"Jane","lastName":"Doe","dateOfBirth":"31-01-1971","email":"jane@example.co","lifetime_value":"$1500.50"}},{"id":"rec132457689","fields":{"firstName":"John","lastName":"Doe","dateOfBirth":"31-01-1973","email":"john@example.co","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))

>Solution :

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":"jlarry@example.co","lifetime_value":"$125500.00"}},{"id":"rec987654321","fields":{"firstName":"Jane","lastName":"Doe","dateOfBirth":"31-01-1971","email":"jane@example.co","lifetime_value":"$1500.50"}},{"id":"rec132457689","fields":{"firstName":"John","lastName":"Doe","dateOfBirth":"31-01-1973","email":"john@example.co","lifetime_value":"$229.00"}}]

To:

json_data = json.loads(response.text)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading