1

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"
        }
    ]
}

2 Answers 2

1

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))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I will be using this URL's with selenium to open them - I have tried to use both of your print commands but I get IndentationError: expected an indented block
@Grimlockz: Do you understand how indentation works in Python?
Just learning python at the moment so need to tab that command to indent it
@Grimlockz: Yes, if you want something to happen inside a loop, you have to indent it one more step than the for statement, like in the examples in the first half of my answer.
0

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

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.