0

I Am new to python and I have a json response (see below). I wanted to search for the value of id for site1 and also list all the values of id.

{ "listSiteResponse" : { "count":4 ,"site" : [  {
  "id": "28e4cc3f-d0c2-46f4-9e0c-b532fd148292",
  "simpleid": 15,
  "name": "Site1",
  "description": "Blr1",
  "location": "Bangalore1",
}, {
  "id": "188d4b47-1955-43e1-82a8-7ccedcfc636b",
  "simpleid": 16,
  "name": "Site2",
  "description": "Blr2",
  "location": "Bangalore2",
}, {
  "id": "63fab512-4b52-4038-8a3b-4632f1911dca",
  "simpleid": 17,
  "name": "Site3",
  "description": "Blr3",
  "location": "Bangalore3",
}, {
  "id": "2db3949a-ba2f-4e93-85b5-24a995fa3d99",
  "simpleid": 18,
  "name": "Site4",
  "description": "Blr4",
  "location": "Bangalore4",
} 
}}

I tried the following script for listing ids but I get an error:

from pprint import pprint
json_data=open('logs/CurrentSitesList.txt')

data = json.load(json_data)
test=data["listSiteResponse"]["site"]["id"]
1
  • Please review my changes to make sure I didn't lose the intent of your post. What is the error your script gives? Commented Oct 2, 2013 at 11:42

1 Answer 1

2

You are trying to get the id of the site of the listSiteResponse, but the problem is that data["listSiteResponse"]["site"] is a list not a single item, and you cannot get the id of a list; instead, you can get the id of all items, or just one item, in that list:

ids = [x["id"] for x in data["listSiteResponse"]["site"]]

Which, without using the compact list comprehension syntax, is equivalent to:

ids = []
for site in data["listSiteResponse"]["site"]:
    ids.append(site["id"])

But the list comprehension syntax is, IMO, much more readable (as well as obviously shorter).

Also, when you open a file, you should make sure it gets closed; so you can either use try-finally or better yet:

with open('logs/CurrentSitesList.txt') as f:
    data = json.load(f)

P.S. you might want to read PEP8 :)

UPDATE: as per the additional comment by the OP in the comments:

given the list of sites

sites = data["listSiteResponse"]["site"]

you might want to find a site with a given ID

site = [x for x in sites if x["id"] == <SPECIFIC_ID>].pop(0, None)
# site will contain either the found object, or None

or find the ID of a site that matches a criterion (generalizing the previous snippet)

site = [x for x in sites if x["name"] == "Site 1"].pop(0, None)
site_id = site["id"] if site else None
# site_id will contain the ID of the matching site, None otherwise

but in the second case, depending on the criteria, you might get multiple results, so adapt your code accordingly if that's the case.

For a more thorough Python tutorial, see the freely available book Learn Python the Hard Way.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution.. Instead of storing this value and comparing using if statement is there a way we can find the id if we give some other unique id . For eg to find the id if we give search parameter as site1

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.