简体   繁体   English

使用 Python 将 JSON 键值对从源映射到目标

[英]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.使用 Python requests ,我想从一个来源获取一块 JSON 并将其发布到目的地。 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?然而,收到的 JSON 的结构与目的地所需的结构有所不同,所以我的问题是,我如何最好地将 map 中的项目从源结构转移到目标结构?

To illustrate, imagine we get a list of all purchases made by John and Mary.举例来说,假设我们得到了 John 和 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:来源 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:目的地 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'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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