0

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 Answers 2

1

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"}}}
Sign up to request clarification or add additional context in comments.

3 Comments

He may want to sort the output by the keys (e.g., [v for k, v in sorted(d["data"]["Response"]["products"].items())]). But otherwise, yeah, this is all there is to it.
Thanks. This is working perfectly for me. Couldn't vote up, as I do not have the credibility.
@user2129946, thank you. You should be able to select it as answer and that will add up to your trust level(votes) as well.
0

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)

3 Comments

Why have you added the deepcopy? And why use a listcomp to do what t.values() already does? (Or list(t.values()) in 3.x.)
@abarnert I used deepcopy just as a precaution, OP is free not to use it. The list comp is bad, Should have just done the t.values()
As a precaution against what? To avoid modifying a temporary object that he has no apparent use for? Or are you planning on iterating over (part of) a while changing b or something (in which case it might be necessary)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.