Grimlockz
Grimlockz

Reputation: 2581

python loop with JSON, rather then manually print out each match

I have successfully written a script to pull information from a json file and parse this in a way in which I will I can use but I at the moment I have to manually print each string. I would like to loop this if possible but stuck on where to start?

Python

from __future__ import print_function
import json
import sys
from pprint import pprint

with open('screen.json') as data_file:    
    data = json.load(data_file)

#json_file.close()

# Print all json data
#pprint(data)
#screen_list = data['screen']
print ("Screens availble",len (data['screen']))

#pprint(data["screen"][1]["id"])
#pprint(data["screen"][1]["user"])
#pprint(data["screen"][1]["password"])
#pprint(data["screen"][1]["code"])


#How to loop this 

print ("https://",data["screen"][0]["server"],"/test/test.php?=",data["screen"][0]["code"],sep='')
print ("https://",data["screen"][1]["server"],"/test/test.php?=",data["screen"][1]["code"],sep='')
print ("https://",data["screen"][2]["server"],"/test/test.php?=",data["screen"][2]["code"],sep='')

JSON

   {
    "screen": [
        {
            "id": "1",
            "user": "[email protected]",
        "password": "letmein",
        "code": "123456",
        "server": "example.com"
        },
        {
            "id": "2",
            "user": "[email protected]",
        "password": "letmein",
        "code": "123455",
        "server": "example.com"
        },
        {
            "id": "3",
            "user": "[email protected]",
        "password": "letmein",
        "code": "223456",
        "server": "example.com"
        }
    ]
}

Upvotes: 1

Views: 177

Answers (2)

abarnert
abarnert

Reputation: 365807

You've already pulled data['screen'] out into a variable named screen_list. That variable is a list, so you can use it the same as any other list—call len on it, index it, or loop over it. So:

screen_list = data['screen']
print("Screens availble", len(screen_list))

for screen in screen_list:
    pprint(screen['id'])
    pprint(screen['user'])
    pprint(screen['password'])
    pprint(screen['code'])

And, down below, just loop again:

for screen in screen_list:
    print("https://", screen["server"], "/test/test.php?=", screen["code"], sep='')

(I'm assuming you want to print out all the screens' info, then print out all the URLs. If you want to print each URL at the same time you print the info, just merge them into one loop.)


As a side note, this is a good time to learn about string formatting. If you want to use those URLs to, e.g., pass them to urllib2 or requests, you can't just print them out, you have to make them into strings. Even if you do just want to print them out, formatting is usually easier to read and harder to get wrong. So:

print("https://{}/test/test.php?={}".format(screen["server"], screen["code"]))

… or …

print("https://{server}/test/test.php?={code}".format(**screen))

Upvotes: 1

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

You can iterate over the list of dicts:

for d in data["screen"]:
    print ("https://",d["server"],"/test/test.php?=",d["code"],sep='')

Out:

https://example.com/test/test.php?=123456
https://example.com/test/test.php?=123455
https://example.com/test/test.php?=223456

Upvotes: 0

Related Questions