Home > Software design >  Is there any way to generate n number of lines with array values
Is there any way to generate n number of lines with array values

Time:04-21

I am having json files and iterating through the each json object

My JSON file :

{
 "User" :{"name":["Eagle","Humming Bird"]}
}

Pythn code:

 for j in json.object:
     print j
 print j

whenever i am runing my python ,output was "Humming Bird"Last object only

I need to get all the objects My expected output :

  Bird : Eagle
  Bird : Humming Bird

if json file has 2 object ,then it generate 2 statement above like this

CodePudding user response:

In your json object, you have one key : "User" whose value is another json/dict , and you are trying to acess the list of birds which is the value of the key : "name" in the inner object.

So basicly you just need to specify that in your code and here's how :

import json
x = '{"User" : {"name":["Eagle","Humming Bird"]} }'
y = json.loads(x)
for bird in y["User"]["name"] :
    print("Bird : ",bird)
  • Related