-1

I have some JSON text I want to iterate through, formatted in the following way:

{
  "itemsPerPage": 45,
  "links": {
    "next": "https://www.12345.com"
  },
  "list": [
    {
      "id": "333333",
      "placeID": "63333",
      "description": " ",
      "displayName": "test-12345",
      "name": "test",
      "status": "Active",
      "groupType": "Creative",
      "groupTypeV2": "Public",
      "memberCount": 1,
    },
     {
      "id": "32423",
      "placeID": "606",
      "description": " ",
      "displayName": "test123",
      "name": "test",
      "status": "Active",
      "groupType": "Creative",
      "groupTypeV2": "Private",
      "memberCount": 1,
    },

I am trying to iterate through this list, and grab the displayName, however my code won't recognize all of the different display names. Here is my code:

for i in range(len(json_obj['list'])):
if (json_obj['list'][i]['displayName'] == "some id"):
    do stuff
else:
    exit()

How can I fix the statement, in order to successfully loop through the json obj?

9
  • This is not a valid json object. Commented Aug 22, 2016 at 17:58
  • what makes it not a valid obj? Commented Aug 22, 2016 at 17:59
  • How can I use this obj, to iterate through and find display name? Commented Aug 22, 2016 at 18:00
  • Why are you referring to it as json_obj and dataset? Are those two variables the same? Commented Aug 22, 2016 at 18:00
  • See here why this is not a valid JSON object Commented Aug 22, 2016 at 18:01

3 Answers 3

0

While the JSON you posted isn't valid, I'll assume you left some stuff off the end.

for entry in dataset['list']:
    print(entry['displayName'])

Will loop through your JSON data.

If you want to do_stuff() if it matches a certain value:

for entry in dataset['list']:
    if entry['displayName'] == 'test-12345':
        do_stuff()
Sign up to request clarification or add additional context in comments.

3 Comments

is entry just a random variable?
Yes - I'm looping through every object in dataset['list'] - on each loop, the current object is named 'entry'. You can call it anything you want.
@pokemongirl1234 Yes. He chose the name entry because each object inside list is an entry.
0

This is work for me.

import json
text = """{
  "itemsPerPage": 45,
  "links": {
    "next": "https://www.12345.com"
  },
  "list": [
    {
      "id": "333333",
      "placeID": "63333",
      "description": " ",
      "displayName": "test-12345",
      "name": "test",
      "status": "Active",
      "groupType": "Creative",
      "groupTypeV2": "Public",
      "memberCount": 1
    },
     {
      "id": "32423",
      "placeID": "606",
      "description": " ",
      "displayName": "test",
      "name": "test",
      "status": "Active",
      "groupType": "Creative",
      "groupTypeV2": "Private",
      "memberCount": 1
    }]}"""
data = json.loads(text)
for item in data['list']:
    if 'displayName' in item:
        print(item['displayName'])

Comments

0

You need to actually perform actions within your loop. Python relies on whitespace to denote blocks. This is something you can't forget when writing Python.

for i in range(len(json_obj['list'])):
if (json_obj['list'][i]['displayName'] == "some id"):
    do stuff
else:
    exit()

should be

for i in range(len(json_obj['list'])):
    if (json_obj['list'][i]['displayName'] == "some id"):
        do stuff
    else:
        exit()

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.