0

I'm trying to loop over a JSON object to check if some entries already exists. my JSON object is like this

[
  {
    "title": "scname",
    "html": "<div><iframe src=/scenario/test3dxblock.0/ width=\"400\" height=\"500\"></iframe></div>",
    "description": "desc",
    "status": "417"
},
{
    "title": "test3dxblock.0",
    "html": "<div><iframe src=/scenario/test3dxblock.0/ width=\"400\" height=\"500\"></iframe></div>",
    "description": "desc",
    "status": "417"
},
{
    "title": "filethumbs.0",
    "html": "<div><iframe src=/scenario/filethumbs.0/ width=\"400\" height=\"500\"></iframe></div>",
    "description": "desc",
    "status": "417"
} 

]

I need to iterate through that and retrieve the title to check if it matches the entries wich i will be adding to this object in case it does not already exists

i'm using requests to generate this object

r = requests.get('http://iframe.ly/api/oembed?url=' + url + '&api_key=' +settings.IFRAMELY_KEY)

        json = r.json()

i've found some answers but nothing seems to do the job, how can i do that ? Thanks

ChemseddineZ
  • 1,728
  • 3
  • 14
  • 27
  • What part of "iterate through that and retrieve the title to check if it matches" do you not know how to do? – FamousJameous Apr 14 '17 at 16:49
  • i need to iterate through the JSON object and retrieve each title wich in my case the titles are **"scname"**, **"test3dxblock.0"**, **"filethumbs.0"** and for each one of those i need to test if it matches another title – ChemseddineZ Apr 14 '17 at 19:58
  • Here are some useful links. For iterating over a list: https://docs.python.org/3/tutorial/controlflow.html#for-statements . For accessing members of a dictionary: https://docs.python.org/3/tutorial/datastructures.html#dictionaries. For comparing strings: http://stackoverflow.com/q/1504717/3901060 – FamousJameous Apr 14 '17 at 20:17

1 Answers1

-1

I would put the JSON data into something called json_string or json_data instead of json so you don't obscure/confuse the name json.

I'm assuming that the data that you show is in string format in your json variable. I copied your data into a variable as multi-line text, but I had to change the backslash-double quote to a single quote in the string. I don't know if you'll need to do that to get json.loads() to work on your data, but if so, then when you need to extract the HTML, you can convert the single quotes back to double quotes. Then I fed it into the json package which makes a list of dictionaries. Then you can extract the titles from that using a list comprehension.

import json

json_string = """[
  {
    "title": "scname",
    "html": "<div><iframe src=/scenario/test3dxblock.0/ width='400' height='500'></iframe></div>",
    "description": "desc",
    "status": "417"
},
{
    "title": "test3dxblock.0",
    "html": "<div><iframe src=/scenario/test3dxblock.0/ width='400' height='500'></iframe></div>",
    "description": "desc",
    "status": "417"
},
{
    "title": "filethumbs.0",
    "html": "<div><iframe src=/scenario/filethumbs.0/ width='400' height='500'></iframe></div>",
    "description": "desc",
    "status": "417"
}
]"""

json_data = json.loads(json_string)
titles = [item['title'] for item in json_data]

When that is pasted into the REPL, titles contains:

['scname', 'test3dxblock.0', 'filethumbs.0']

If the variable you show a json is actually a list already, then you just need to use the list comprehension to get the titles. If you have a new title that you want to see if it is in the list titles, you can just say:

if title in titles:
    # do whatever with it
else:
    # add it to list - or whatever
PurpleDiane
  • 1,693
  • 1
  • 12
  • 15
  • Please explain why my answer is marked down? It seems quite unfair to mark it down without giving a reason. Did I misunderstand the question? – PurpleDiane Apr 16 '17 at 18:34