简体   繁体   中英

Convert JSONObject into JSONArray using Python

I have gone through various threads, but couldn't find the particular answer in python. I have a json file

{
    "StoreID" : "123",
    "Status" : 3,
    "data" : {
            "Response" : {
                    "section" : "25",
                    "elapsed" : 277.141,
                    "products" : {
                            "prd_1": {
                                    "price" : 11.99,
                                    "qty" : 10,
                                    "upc" : "0787493"
                            },
                            "prd_2": {
                                    "price" : 9.99,
                                    "qty" : 2,
                                    "upc" : "0763776"
                            },
                            "prd_3": {
                                    "price" : 29.99,
                                    "qty" : 8,
                                    "upc" : "9948755"
                            }
                    },
                    "type" : "Tagged"
            }
    }
}

I need to convert this json file into the format below, by changing json object 'products' into an array form.

{
    "StoreID" : "123",
    "Status" : 3,
    "data" : {
            "Response" : {
                    "section" : "25",
                    "elapsed" : 277.141,
                    "products" : [
                            {
                                    "price" : 11.99,
                                    "qty" : 10,
                                    "upc" : "0787493"
                            },
                            {
                                    "price" : 9.99,
                                    "qty" : 2,
                                    "upc" : "0763776"
                            },
                            {
                                    "price" : 29.99,
                                    "qty" : 8,
                                    "upc" : "9948755"
                            }
                    ],
                    "type" : "Tagged"
            }
    }
}

Is there any good way to do it in python. Mostly I saw people are using java, but not in python. Can you please let me know a way to do it in python.

Just get the values() of products dictionary and that will give you an array of values. Code below works from me assuming your json is in file1.txt Also note

import json
with open('file1.txt') as jdata:
    data = json.load(jdata)
    d = data
d["data"]["Response"]["products"] = d["data"]["Response"]["products"].values()
print(json.dumps(d))

output:

{"Status": 3, "StoreID": "123", "data": {"type": "Tagged", "Response": {"section": "25", "products": [{"price": 9.99, "upc": "0763776", "qty": 2}, {"price": 29.99, "upc": "9948755", "qty": 8}, {"price": 11.99, "upc": "0787493", "qty": 10}], "elapsed": "277.141"}}}

Would something like this work for you?

import json
import copy

a = json.load(open("your_data.json", "r"))

b = copy.deepcopy(a)

t =  a.get('data').get('Response').get('products')
b['data']['Response']['products'] = t.values()    # Originally was: [t[i] for i in t]

You can give back JSON with json.dumps(b)

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