user3472065 user3472065 - 9 days ago 5
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": "http://192.168.10.1/index.php",
"allow": "no",
"to": 10
},
{
"from": 20,
"my_ref": "http://192.168.10.2/index.php",
"allow": "mandatory",
"to": 0
}
],
"comment": "My PHP",
"identifiable_with_user": true,
"key": 10,
"link": [
{
"href": "http://192.168.10.1/index.php",
"method": "GET",
"rel": "self",
"type": "website"
},
{
"href": "http://192.168.10.5/identifiable.php",
"method": "GET",
"rel": "account_info"
}
],
"name": "Accounts",
"read_only": true,
"system": true,
"system_key": 20,
"tls_match_ref": "http://192.168.10.5/accounts.php"
}


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

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:
        parse(e)
    else:
      if 'ref' in k:
        print(k + ':' + v)


json_data = '{\
  "objects": [\
    { "from": 44, "my_ref": "http://192.168.10.1/index.php", "allow": "no", "to": 10 },\
    { "from": 20, "my_ref": "http://192.168.10.2/index.php", "allow": "mandatory", "to": 0 }\
  ],\
  "comment": "My PHP",\
  "identifiable_with_user": true,\
  "key": 10,\
  "link": [\
    { "href": "http://192.168.10.1/index.php", "method": "GET", "rel": "self", "type": "website" },\
    { "href": "http://192.168.10.5/identifiable.php", "method": "GET", "rel": "account_info" }\
  ],\
  "name": "Accounts",\
  "read_only": true,\
  "system": true,\
  "system_key": 20,\
  "tls_match_ref": "http://192.168.10.5/accounts.php"\
}'

data = json.loads(json_data)
parse(data)

Output :

href:http://192.168.10.1/index.php
href:http://192.168.10.5/identifiable.php
my_ref:http://192.168.10.1/index.php
my_ref:http://192.168.10.2/index.php
tls_match_ref:http://192.168.10.5/accounts.php
Comments