-1

I dont know how to iterate these json file using forloop i tried this it printing all content but i need to print only the id and class.

for (k, v) in bin3.items():  
if k == 'ID':
    print(v)

The above code doesnt print anything.

this is my json file content { "content": { "ID": "stringIdentity:@5", "class": 1, "annotations": [ { "ID": 1, "class": 2, "body": "" }]}}

dir_with_bin_folder=[]
for root, directories, files in os.walk(directory): 
    for filename in files:
        if filename=='@3.bin':
            with open(root+'/'+filename) as json_file:
                bin3 = json.load(json_file)
                df = pd.read_json(root+'/'+filename)
                print(filename)
                print(bin3)
                annotations=bin3['annotations']
                bin3_content=(bin3['content'])
                bin3_IID=(bin3['ID')
                bin3_class=(bin3['class'])
                for i in annotations:
                bin3_ID=(i['ID'])
                bin3_class=(i['class'])
                bin3_body=(i['body'])
                print(bin3_ID)

And i tried this above one also but im getting keyvalue error

3
  • json can be loaded as python dict. Then the contents can be accessed trivially. What content are you trying to extract? Commented Feb 28, 2020 at 6:32
  • I don't see anything that you can loop over in this JSON... You need an iterable to loop over (like a list), not a JSON/dictionary. Commented Feb 28, 2020 at 6:32
  • Maybe you want to do something like this? Commented Feb 28, 2020 at 6:34

1 Answer 1

2

The json file is only iterable from the annotations key contained in context. If you want to print the ID and class from annotations you will need to loop over the annotations list and get the the value where the keys equals 'ID' and 'class'.

with open(root+'/'+filename) as json_file:
    bin3 = json.load(json_file)

for annotation in bin3['content']['annotations']:
    id_value = annotation.get('ID',None)
    class_value = annotation.get('class',None)
    print(id_value, class_value, sep=' ')
Sign up to request clarification or add additional context in comments.

5 Comments

I have a doubt if im having a one more list ['transformations'] inside that 'annotations' how can i get details from that? this annotations and transformations are keys inside the content. so how can i give this foreg : bin3 ['content']['annotations']['transformations'] or nested for loop?
You can then just loop through that tag also within the current annotation loop. For tranformation in annotation['tranformations']:
im getting keyerror : transformations i just tried like that for annotation in bin3 ['content']['annotations']: print(annotation) for transformation in annotation['transformations']: print(transformation)
Ok so lets assume your json looks like this now: bin3 = {"content": { "ID": "stringIdentity:@5", "class": 1, "annotations": [ { "ID": 1, "class": 2, "body": "", "transformations":[ { "ID": 1, "class": 2, "body": "" } ] }]}}
Then this should work: for annotation in bin3['content']['annotations']: id_value = annotation.get('ID',None) class_value = annotation.get('class',None) for transformation in annotation.get('transformations',None): trans_id_value = transformation.get('ID', None) trans_class_value = transformation.get('class', None)

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.