-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

  • json can be loaded as python `dict`. Then the contents can be accessed trivially. What content are you trying to extract? – MjZac Feb 28 '20 at 06: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. – Prateek Dewan Feb 28 '20 at 06:32
  • Maybe you want to do something like [this](https://stackoverflow.com/a/15477213/12730306)? – Federico Rossi Feb 28 '20 at 06:34

1 Answers1

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=' ')
Lambo
  • 1,094
  • 11
  • 18
  • 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? – priyanka madhesh Mar 04 '20 at 12:33
  • You can then just loop through that tag also within the current annotation loop. For tranformation in annotation['tranformations']: – Lambo Mar 05 '20 at 05:31
  • im getting keyerror : transformations i just tried like that for annotation in bin3 ['content']['annotations']: print(annotation) for transformation in annotation['transformations']: print(transformation) – priyanka madhesh Mar 05 '20 at 06:46
  • 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": "" } ] }]}} – Lambo Mar 05 '20 at 06:50
  • 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) – Lambo Mar 05 '20 at 06:50