0

I have this json file loaded in Python with json.loads('myfile.json'):

    [
    {
        "cart": {
            "items": {
                "3154ba405e5c5a22bbdf9bf1": {
                    "item": {
                        "_id": "3154ba405e5c5a22bbdf9bf1",
                        "title": "Drink alla cannella",
                        "price": 5.65,
                        "__v": 0
                    },
                    "qty": 1,
                    "price": 5.65
                }
            },
            "totalQty": 1,
            "totalPrice": 5.65
        }
    },
    {
        "cart": {
            "items": {
                "6214ba405e4c5a31bbdf9ad7": {
                    "item": {
                        "_id": "6214ba405e4c5a31bbdf9ad7",
                        "title": "Drink alla menta",
                        "price": 5.65,
                        "__v": 0
                    },
                    "qty": 2,
                    "price": 11.3
                }
            },
            "totalQty": 2,
            "totalPrice": 11.3
        }
    }
]

How I can access to both totalQty and totalPrice fields at same time and sum them?

How I can access to both Title fields to print it?

3
  • 1
    sum(dct["cart"]["totalQty"] for dct in my_json) and sum(dct["cart"]["totalPrice"] for dct in my_json)? Commented Feb 25, 2022 at 14:40
  • 1
    How did you manage to read a file with json.loads() ? Commented Feb 25, 2022 at 14:41
  • Thanks a lot Axe, I have added one more question, How i can access to Title field? Commented Feb 25, 2022 at 14:45

1 Answer 1

2

Let's assume that you have the JSON data available as a string then:

jdata = '''
[
    {
        "cart": {
            "items": {
                "3154ba405e5c5a22bbdf9bf1": {
                    "item": {
                        "_id": "3154ba405e5c5a22bbdf9bf1",
                        "title": "Drink alla cannella",
                        "price": 5.65,
                        "__v": 0
                    },
                    "qty": 1,
                    "price": 5.65
                }
            },
            "totalQty": 1,
            "totalPrice": 5.65
        }
    },
    {
        "cart": {
            "items": {
                "6214ba405e4c5a31bbdf9ad7": {
                    "item": {
                        "_id": "6214ba405e4c5a31bbdf9ad7",
                        "title": "Drink alla menta",
                        "price": 5.65,
                        "__v": 0
                    },
                    "qty": 2,
                    "price": 11.3
                }
            },
            "totalQty": 2,
            "totalPrice": 11.3
        }
    }
]
'''
totalQty = 0
totalPrice = 0
for d in json.loads(jdata):
    c = d['cart']
    totalQty += c['totalQty']
    totalPrice += c['totalPrice']
    for sd in c['items'].values():
        print(sd['item']['title'])
print(f'{totalQty:d}', f'{totalPrice:.2f}')

Output:

3 16.95

Note:

I suspect that what you really want to do is multiply those two values

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

4 Comments

Thanks for the help, I need to sum the two totalQty and the two TotalPrice, but it is the same. But if I want to access to Title fields??
@Alessio That's clearer regarding the summation. I'll edit the code
Thanks alot and there is a way to access into Title fields with the same json??
@Alessio I have added that to the answer

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.