0

Thanks.

Am trying to get the particular key value from json data using python. logic is to call the function and function should return key (json output sometimes will be just 1 index data or sometime morethan 1 index data)

I was able to get the data and print them, it works fine in inside for loop but when I return back to main then am getting only one value. not sure if something to do with for loop.

json data :

    [{
    id: "587e569472",
    hostname: "I-56BXX",
    env: "Beta",
    site: "I",
    version: "2.0.0.38-1"},
    {
    id: "587e64472",
    hostname: "I-56AXX",
    env: "Beta",
    site: "I",
    version: "2.0.0.39-1"}]

main script :

    def get_jsondata(url, hosts):
        u = urllib2.urlopen(url)
        json_object = json.load(u)
        u.close
        indexcount = len(json_object)
        #print indexcount
        #for i in json_object:
            #print i['hostname']

        if json_object == []:
            print 'No Data!'
        else:
            for rows in json_object:
                #print 'hostname:' + rows['hostname']
                #print 'env:' + rows['env']
                print 'hostname and its env:' + rows['hostname'] + " " + rows['env']
                #return rows['hostname']
                hosts = rows['hostname']
                #print hosts
                return (hosts)

    #if __name__ == '__main__':
    #    main()

#main section
    url = 'http://api.com/AppData/'
    hosts = ""
    hosts = get_jsondata(url, hosts)
    #print "The required hostname " + str(hostname) + " and its env is " + str(venue)
    print(hosts)

After running the script am getting output as :

hostname and its env:I-56BXX I
I-56BXX

I was trying to get both hostname return back to main so, output would be like

hostname and its env:I-56BXX I
hostname and its env:I-56AXX I
I-56BXX
I-56AXX

first 2 line from above output is from print stmt inside for loop and next 2 lines are from return data.

2
  • hosts = [h['hostname'] for h in json_object] Commented Apr 28, 2017 at 18:48
  • You are retuning from the function in the first interation of the loop Commented Apr 28, 2017 at 18:48

4 Answers 4

2

Well, your return statement is inside the loop, so it return on the first iteration, you can use thing like yield or stocking your result into a list you would return at the end of the function

someting like

return [row['hostname'] for row in json_object]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Madahin. Good to know this logic.
0

The reason you are only printing one record in json_object is because you are returning hosts too soon. If you remove the line return (hosts) from inside the for loop, then all records from json_object will be printed.

The format that you are hoping to end up with will require a little more work. The format that will be printed will look like this:

hostname and its env:I-56BXX I
I-56BXX
hostname and its env:I-56AXX I
I-56AXX

If you would like to print in your stated format, you should have one for loop that prints the first message and a second for loop to print the second message.

Comments

0

Create an empty list, append to the list, then move your return outside the loop. The other answers are cleaner and great, but here is the most direct fix of your code:

def get_jsondata(url, hosts):
    u = urllib2.urlopen(url)
    json_object = json.load(u)
    u.close
    indexcount = len(json_object)
    #print indexcount
    #for i in json_object:
        #print i['hostname']

    if json_object == []:
        print 'No Data!'
    else:
        hosts = []
        for rows in json_object:
            #print 'hostname:' + rows['hostname']
            #print 'env:' + rows['env']
            print 'hostname and its env:' + rows['hostname'] + " " + rows['env']
            #return rows['hostname']
            hosts.append(rows['hostname'])
            #print hosts
        return (hosts)

Comments

0

Please do not post code with comment on StackOverflow: it's hard to read. Keep your snippets simple. Plus, your code is not very clean (parenthesis at return statement, inconsistency with parenthesis on print statements, missing parenthesis on function calls u.close()...)

With that said, get_jsondata only output one value because of your return statement in your for loop. The return instruction breaks the loop during the first iteration.

Now I didn't get what the get_jsondata is supposed to return. By reading your code I guess it's a list of hostnames. In order to return the list of all the hostnames in your json data, you can use one powerful feature of python: list comprehension.

e.g

[x['hostname'] for x in json_object]

Comments

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.