python - Iterate through JSON object to check if some entries already exists -
i'm trying loop on json object check if entries exists. json object 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 iterate through , retrieve title check if matches entries wich adding object in case not exists
i'm using requests generate object
r = requests.get('http://iframe.ly/api/oembed?url=' + url + '&api_key=' +settings.iframely_key) json = r.json()
i've found answers nothing seems job, how can ? thanks
i put json data called json_string
or json_data
instead of json
don't obscure/confuse name json
.
i'm assuming data show in string format in json
variable. copied data variable multi-line text, had change backslash-double quote
single quote in string. don't know if you'll need json.loads() work on data, if so, when need extract html, can convert single quotes double quotes. fed json package makes list of dictionaries. can extract titles using 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'] item in json_data]
when pasted repl, titles contains:
['scname', 'test3dxblock.0', 'filethumbs.0']
if variable show json
list already, need use list comprehension titles. if have new title
want see if in list titles
, can say:
if title in titles: # whatever else: # add list - or whatever
Comments
Post a Comment