2

I have a json file with the following example json entry:

{
            "title": "Test prod",
            "leafPage": true,
            "type": "product",
            "product": {
                "title": "test product",
                "offerPrice": "$19.95",
                "offerPriceDetails": {
                    "amount": 19.95,
                    "text": "$19.95",
                    "symbol": "$"
                },
                "media": [
                    {
                        "link": "http://www.test.com/cool.jpg",
                        "primary": true,
                        "type": "image",
                        "xpath": "/html[1]/body[1]/div[1]/div[3]/div[2]/div[1]/div[1]/div[1]/div[1]/a[1]/img[1]"
                    }
                ],
                "availability": true
            },
            "human_language": "en",
            "url": "http://www.test.com"
        }

I can post via python script this to my test server perfectly when I use:

                     "text": entry.get("title"),
                     "url": entry.get("url"),
                     "type": entry.get("type"),

However I cannot get the following nested item to upload the values, how do I structure the python json call to get a nested python json entry?

Ive tried the below without success, I need to have it as .get because there are different fields currently in the json file and it errors out without the .get call.

                 "Amount": entry.get("product"("offerPrice"))

Any help on how to structure the nested json entry would be very much appreciated.

1 Answer 1

5

You need to do:

"Amount": entry.get("product", {}).get("offerPrice")

entry.get("product", {}) returns a product dictionary (or an empty dictionary if there is no product key).

Sign up to request clarification or add additional context in comments.

2 Comments

Great thanks thats worked! so to get a nested object of products{ media I could use: entry.get("product", {}).get("media", {}).get("link") ?
Since media is a list then: entry.get("product", {}).get("media", [{}])[0].get("link")

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.