0
votes

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.

2

2 Answers

1
votes

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"}}}
0
votes

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)