3

I am new here, this is my first question. We all start somewhere :D

I have this data being returned to me via an API and I store it in a variable named jsonResponseData

{
    "date-value": [{
        "dateTime": "2020-06-25",
        "value": "34365"
    }, {
        "dateTime": "2020-06-26",
        "value": "7268"
    }, {
        "dateTime": "2020-06-27",
        "value": "4762"
    }, {
        "dateTime": "2020-06-28",
        "value": "4650"
    }, {
        "dateTime": "2020-06-29",
        "value": "2934"
    }, {
        "dateTime": "2020-06-30",
        "value": "4973"
    }, {
        "dateTime": "2020-07-01",
        "value": "3594"
    }, {
        "dateTime": "2020-07-02",
        "value": "19674"
    }]
}

Is this even a nested dictionary? I am fairly new to programming and any help is welcome! Regardless of what it is, how would I parse it so I only keep the 7 value values and am able to then add them up into one number.

1
  • Are you going to get sum of these fields? "value": "4650" Commented Jul 2, 2020 at 18:32

6 Answers 6

3

You can iterate through the list, and access the dict inside.

for entry in data['date-value']:
    print(entry['value'])
    # or whatever else you want to do with the values
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much! Works like a charm. I will accept this as an answer in 10 minutes once it lets me. One more question: Is a nested dictionary the correct terminology?
I would have called it a list of dictionaries. (Although they are technically nested inside 'date-value', we access the list directly)
2
values = [x["value"] for x in jsonResponseData["date-value"]]

List Comprehension

Comments

2

Yes, this is a nested dictionary. Instead of using a loop, you can use list comprehension.

>>> sum(int([inner['value']) for inner in my_dict['date-value']])
82220

For each nested dictionary in my_dict, turn the 'value' element into an integer and sum the results.

Comments

2

You can use this function which will return the sum of the values. Hope this helps!

def getValuesSum(json_data):
    values_list=[int(items['value']) for items in json_data['date-value']]
    return sum(values_list)

Comments

1

This is one way to extract those values (given that the object above is called dict).

for val in dict["date-value"]:
  print(val["value"])

Comments

1

Is this even a nested dictionary?

Sort of. It's a dictionary with a list of dictionaries inside it.

Regardless of what it is, how would I parse it so I only keep the 7 value values and am able to then add them up into one number.

Try this:

import json

data = """
{
    "date-value": [{
        "dateTime": "2020-06-25",
        "value": "34365"
    }, {
        "dateTime": "2020-06-26",
        "value": "7268"
    }, {
        "dateTime": "2020-06-27",
        "value": "4762"
    }, {
        "dateTime": "2020-06-28",
        "value": "4650"
    }, {
        "dateTime": "2020-06-29",
        "value": "2934"
    }, {
        "dateTime": "2020-06-30",
        "value": "4973"
    }, {
        "dateTime": "2020-07-01",
        "value": "3594"
    }, {
        "dateTime": "2020-07-02",
        "value": "19674"
    }]
}
"""

data_dict = json.loads(data)

values = 0
for x in data_dict["date-value"]:
    values += int(x['value'])

print(values)

That should result in values being the sum of all 7 numbers.

Comments

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.