简体   繁体   中英

Mapping JSON key-value pairs from source to destination using Python

Using Python requests I want to grab a piece of JSON from one source and post it to a destination. The structure of the JSON received and the one required by the destination, however, differs a bit so my question is, how do I best map the items from the source structure onto the destination structure?

To illustrate, imagine we get a list of all purchases made by John and Mary. And now we want to post the individual items purchased linking these to the individuals who purchased them ( NOTE : The actual use case involves thousands of entries so I am looking for an approach that would scale accordingly):

Source JSON:

{
    'Total Results': 2, 
    'Results': [
        {
            'Name': 'John',
            'Age': 25,
            'Purchases': [
                {
                    'Fruits': {
                        'Type': 'Apple',
                        'Quantity': 3,
                        'Color': 'Red'}
                        }, 
                {
                'Veggie': {
                    'Type': 'Salad', 
                    'Quantity': 2, 
                    'Color': 'Green'
                    }
                }
            ]
        },
        {
            'Name': 'Mary',
            'Age': 20, 
            'Purchases': [
                {
                    'Fruits': {
                        'Type': 'Orange',
                        'Quantity': 2,
                        'Color': 'Orange'
                    }
                }
            ]
        }
    ]
}

Destination JSON:


{
    [
        {
            'Purchase': 'Apple', 
            'Purchased by': 'John',
            'Quantity': 3, 
            'Type': 'Red',
        }, 
        {
            'Purchase': 'Salad', 
            'Purchased by': 'John', 
            'Quantity': 2, 
            'Type': 'Green',
        },
        {
            'Purchase': 'Orange', 
            'Purchased by': 'Mary',
            'Quantity': 2, 
            'Type': 'Orange',
        }
    ]
}

Any help on this would be greatly appreciated! Cheers!

Just consider loop through the dict.

res = []

for result in d['Results']:
    value = {}
    for purchase in result['Purchases']:
        item = list(purchase.values())[0]
        value['Purchase'] = item['Type']
        value['Purchased by'] = result['Name']
        value['Quantity'] = item['Quantity']
        value['Type'] = item['Color']
        res.append(value)
pprint(res)

[{'Purchase': 'Apple', 'Purchased by': 'John', 'Quantity': 3, 'Type': 'Red'},
 {'Purchase': 'Salad', 'Purchased by': 'John', 'Quantity': 2, 'Type': 'Green'},
 {'Purchase': 'Orange', 'Purchased by': 'Mary', 'Quantity': 2, 'Type': 'Orange'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM