0

JSON File: http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json I'm using Python 2.

I am trying to extract all the 'paper_item_id' 's from the JSON file (specified above), using a loop and storing the 'paper_item_id' in a 'item_id' variable, so this variable will change each time the loop iterates to the 'paper_item_id', but also I want to have an if statement which checks if the 'paper_item_id' in the JSON file 'is_bait' is 'true' if it is true the the 'item_id' variable will not store the 'paper_item_id' which has an 'is_bait' of true and go on to the next one.

Step 1) Get JSON Data.
Step 2) Filter out 'paper_item_id' 's with the 'is_bait' to true.
Step 3) Run a loop which assigns a 'item_id' variable to the 'paper_item_id' received.
Step 4) The loop should run so all filtered 'paper_item_id' (item_id) has been passed to 'myFunction'

Sample English Like Code:

    foreach ('don't know what to have for the loop cond') {
        item_id = 'paper_item_id'
        if (item_id[is_bait]) == true  {
            code which will go to the end of the loop    
        }

        else 
        {
        myFunction(item_id)
        }

I know this has a Javascript kind-of syntax but I want it in python.

What I have now:

import json
import urllib2
url = 'http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json'
result = json.loads(url)
request = urllib2.Request(url)
response = urllib2.urlopen(request)
json_obj = json.load(response)

What do I do know?

1
  • Your JSON contains an error. You will need to adjust for this at some point. Commented Jan 4, 2015 at 16:13

3 Answers 3

2

I've used requests.get, and also checked for a valid HTTP response.

Here's my sample code:

import json
import requests

def myFunction(item):
    # do something here
    print item['paper_item_id']
    pass

json_obj = requests.get('http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json').json()


for item in json_obj:
    if 'is_bait' in item and item['is_bait'] == "1":
        # item['is_bait'] == "1", in case you ever get is_bait = "0" in your json response.
        print item['paper_item_id']
        continue
    else:
        myFunction(item)
Sign up to request clarification or add additional context in comments.

2 Comments

i just want the paper_item_id number though
just edited code to show you how to get paper_item_id
1

Here is the translation of what you give as a pseudo code:

import json
import urllib2
url = 'http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json'
result = json.loads(url)
request = urllib2.Request(url)
response = urllib2.urlopen(request)
json_obj = json.load(response)
for item in json_obj:
    if "is_bait" in item and item['is_bait']:
        continue
    else:
        # do stuff

The continue can be skipped if you reverse the condition though.

3 Comments

Nope... let's not mislead OP
All I want is the paper item id's value, so i just want the numbers
The code will not work as posted. See my answer.
0

json.load will read your data into a list of dictionaries, as appropriate. After that, you can filter on whatever it is your heart desires using standard python object manipulation:

import json
import urllib2
url = 'http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json'
result = json.load(url)
request = urllib2.Request(url)
response = urllib2.urlopen(request)
json_obj = json.load(response)

Your issue is that you're reading the URL object as a string and that's a non-starter. I fixed your code above and it now reads in the whole thing (snippet from output below:

 {u'cost': 0,
  u'is_bait': u'1',
  u'is_member': Terue,
  u'label': u"Stompin' Bob Fowhawk Hair",
  u'layer': 6000,
  u'paper_item_id': 1274,
  u'prompt': u"Stompin' Bob Fowhawk Hair",
  u'type': 2},
 {u'cost': 0,
  u'is_bait': u'1',
  u'is_member': True,
  u'label': u'G Billy Hair and Bandana',
  u'layer': 6000,
  u'paper_item_id': 1275,
  u'prompt': u'G Billy Hair and Bandana',
  u'type': 2},
  ... continues for a long time...

2 Comments

You can also convert the JSON into the correct format by doing str_json = request.read().decode('utf-8') and loading this instead.
while reading, can you filter out objects using object_hook in json.load method?

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.