1

I have a scenario where I am looping through a json response and trying to have a mapped key value pair. objective : loop through the json file and search for values with name content1 inside the labels object if found then return the corresponding value of name(TEST API1) and id (33332) and store it in a variable or list to further store it in csv file with format name and id and content name and adding a sl num.

Here is what I have tried already:

get_metadata = requests.get('https://testurl.com')
metadata = get_metadata.json()
for key in metadata:
    value = metadata[i]['labels']
    print('The key and value are ({}) = ({})'.format(key, value))

Here error occurs:

Error: `TypeError: the JSON object must be str, bytes or bytearray, 
not list`

How can I achieve the above objective what should be the approach any help would be great. If anybody can help here how to proceed further in this scenario.

2 Answers 2

1

Try this one:

for dict_ in metadata:
    for label in dict_['labels']:
        if label['content'] == 'content1':
           print('The key and value are ({}) = ({})'.format(dict_['id'], 
dict_['name']))

The output:

The key and value are (33332) = (TEST API1)

If you want to export result as CSV file, try this:

import csv

with open('objects.csv', 'w') as csvfile:
    filewriter = csv.writer(
        csvfile, 
        delimiter=',',
        quotechar='|', 
        quoting=csv.QUOTE_MINIMAL
    )
    for dict_ in metadata:
        for label in dict_['labels']:
            if label['content'] == 'content1':
                filewriter.writerow([dict_['id'], dict_['name']])
Sign up to request clarification or add additional context in comments.

1 Comment

just wanted to understand if I wan to filter something like this ABE content1 and I have another value as well which has ABC content1 out of which I just only want to filter out values for ABE content1 How do I do that ? because content1 is a common term that is associated with both contents but I want to only filter out for ABE content1
0

You can try

>>> for key in metadata:
...     name = key['name']
...     label_info = key['labels']
...     for x in label_info:
...             print(f"{name}, {x['id']}, {x['content']}")
...
TEST API1, 667, content1
TEST API1, 668, content2
TEST API1, 669, content3
TEST API1, 112, content4
TEST API2, 668, content2
TEST API2, 669, content3
TEST API2, 112, content4

4 Comments

Thanks for the response, but I need to check if there is content as content1 in the json available if yes then only I need to print the name, id and content value of that corresponding. This code what you have given prints everything
then you just need an if statement in the inner for loop if x['content'] == 'content1'
Cool , thanks but how to I store this data in CSV file ?
Just wanted to understand if I wan to filter something like this ABE content1 and I have another value as well which has ABC content1 How do I do that ? beacause content1 is a common term that is associated with both contents but I want to only filter out for ABE content1

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.