user3472065 user3472065 - 1 year ago 60
JSON Question

In Python parse a generic JSON structure looking for specific values

I'm trying to parse a JSON structure and looking for specific values.

Here an example of a possibile data to parse:

"objects": [
"from": 44,
"my_ref": "",
"allow": "no",
"to": 10
"from": 20,
"my_ref": "",
"allow": "mandatory",
"to": 0
"comment": "My PHP",
"identifiable_with_user": true,
"key": 10,
"link": [
"href": "",
"method": "GET",
"rel": "self",
"type": "website"
"href": "",
"method": "GET",
"rel": "account_info"
"name": "Accounts",
"read_only": true,
"system": true,
"system_key": 20,
"tls_match_ref": ""

I need to extract all the values identified by the key *ref (like my_ref, href, etc...) in a generic JSON structure. I just need to extract the URL and then perform some actions:

for entities in JSON_structure
URL= "take the URL corresponding to the ref key"
so something with the URL

I tried using .items() filtering "http://" string but didn't work

URL = {k:v for (k,v) in element_detail.items() if "http://" in k or v}

Answer Source

You should parse the data then look for the keys with ref in it.

That does the trick :

import json

def parse(root):
  for k,v in root.items():
    if isinstance(v, list):
      for e in v:
      if 'ref' in k:
        print(k + ':' + v)

json_data = '{\
  "objects": [\
    { "from": 44, "my_ref": "", "allow": "no", "to": 10 },\
    { "from": 20, "my_ref": "", "allow": "mandatory", "to": 0 }\
  "comment": "My PHP",\
  "identifiable_with_user": true,\
  "key": 10,\
  "link": [\
    { "href": "", "method": "GET", "rel": "self", "type": "website" },\
    { "href": "", "method": "GET", "rel": "account_info" }\
  "name": "Accounts",\
  "read_only": true,\
  "system": true,\
  "system_key": 20,\
  "tls_match_ref": ""\

data = json.loads(json_data)

Output :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download