1

I'm having trouble trying to get a list of values from a specific key inside an json array using python. Using the JSON example below, I am trying to create a list which consists only the values of the price key. Original JSON:

{
   "data": 
       [
           {
               "id": 72292977, 
               "name": "Smart Cook inox 316 410ml EDA0309", 
               "price": 199000 
           }, 
           {
               "id": 73124602, "sku": "7494611465205", 
               "name": "Lock&Lock Vacuum Bottle LHC6180SLV (800ML)", 
               "price": 293000
           }
       ]
   "paging": 
       {
           "total": 1876, 
           "per_page": 48, 
           "current_page": 1
       }
}

Expected:

["199000 ", "293000"]

Below is the code of my approach:

import json
try:
    with open("data.json", 'r') as f:
        contents = json.load(f)
except Exception as e:
    print(e)

How can I get the price value after loading json?

1 Answer 1

1

This is similar to the answer above, but it gets the information from the json file itself, rather than pasting the file as a variable.

import json

# Open json export file
with open('data.json', 'r') as f:
    contents = json.load(f)

data = contents['data']

prices = []

for id in data:
    prices.append(id['price'])

print(prices)

Output:

[199000, 293000]
Sign up to request clarification or add additional context in comments.

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.