asp
asp

Reputation: 855

Not able to read value from json object

Problem Statement: Need to check if value present in json record and if yes, return true as output as variable using Python 3.x

The code that I am trying to use:

res = conn.getresponse()
data = res.read()
outputdata = data.decode("utf-8")
outputdata1 = json.loads(outputdata)
for key,value in outputdata1.items():
  print(key, value)

Output I get:

next_page_token None
items [{'name': 'pythontest34', 'id': '0126ffc8-5656-423e-b7fe-56d4e93a80d6', 'created_at': '2022-06-16T04:37:32.958Z'}, {'name': 'avengers', 'id': '41541893-f916-426b-b135-c75898759b0b3', 'created_at': '2022-06-24T08:39:39.806Z'}, {'name': 'abkalkib217', 'id': '4cc606f1-749f-4e5d-9d76-41460dc9a578', 'created_at': '2022-06-24T15:11:17.145Z'}, {'name': 'Common', 'id': '7bf46575-f02a-44fc-8b65-d596d8f1ba30', 'created_at': '2022-06-21T06:11:11.102Z'}, {'name': 'ParameterTest', 'id': 'c4eff567-a6af-4a7f-b907-60ae98d4925a', 'created_at': '2022-06-01T09:55:04.944Z'}, {'name': 'ab-synpase', 'id': 'c9655981-70f8-43d8-ba8e-7f16395ef969', 'created_at': '2022-05-18T18:24:04.526Z'}]

What I am trying to achieve:

If I pass name as python runtime argument(sys.argv) it should return "True" if this value in above output

How to get into items and get this name based on runtime parameter

Any help would be highly appreciated.

Upvotes: 0

Views: 62

Answers (2)

asp
asp

Reputation: 855

Thanks for all suggestions.. @MatsLindh @Xingzhou Liu @Chinmay T the following worked. I was figuring out how to return false if value does not exist for a given key. Unless I declare variable(status) a default value "False" it was returning empty value. I need to consume this true or false in next workflow job.

  status = "False"
    outputdata1 = json.loads(outputdata)
    for token in outputdata1['items']:
        for key, value in token.items():
            #print(key,value)
            if value == "avengers":
                status = "True"
   print(status)

Upvotes: 0

Chinmay T
Chinmay T

Reputation: 1133

You have parsed data using json.loads() which returns results as Python dictionary. check here

I have added sample json data and one of the way to navigating json data structure and filter data based on field or it's value.

test_array = '[{"id": 1, "date": "2021-06-01T12:45:30.9861524-04:00", "temperatureC": 39, "temperatureF": 102, "summary": "Sweltering"}, {"id":2, "date": "2021-06-02T12:45:30.9861607-04:00", "temperatureC": -12, "temperatureF": 11, "summary": "Scorching"}, {"id":3,"date": "2021-06-03T12:45:30.9861613-04:00", "temperatureC": 50, "temperatureF": 121, "summary": "Chilly"}, {"id":4,"date": "2021-06-04T12:45:30.9861617-04:00", "temperatureC": 51, "temperatureF": 123, "summary": "Chilly"},{"id":5,"date": "2021-06-05T12:45:30.9861647-04:00", "temperatureC": 3, "temperatureF": 37, "summary": "Hot"}]'

test_array_to_json = json.loads(test_array)

for item in test_array_to_json:
    print('data row:',item)
    for itemkey in item.keys():
        print('data column key:',itemkey,'data column value:',item[itemkey])
        #If you wan to filter on field only
        if itemkey == "summary" :
            print('filtered data key',itemkey, 'filtered data value',item[itemkey])
        #If you want to filter on field and it's value    
        if (itemkey == "summary" and item[itemkey] =="Chilly") :
            print('filtered data key',itemkey, 'filtered data value',item[itemkey])

Upvotes: 1

Related Questions