简体   繁体   中英

Extract values from json-file which has no unique markers

A json-file which has unique markers (or [more appropriate] field-names) preceeding the values is (rather) easy to dissect, because you can perform a string search on the unique markers/field-names to find within the string the first and last position of the characters of the value, and with that info you can pinpoint the position of the value, and extract the value. Have performed that function with various lua-scripts and Python-scripts (also on xml-files).

Now need to extract values from a json-file which does not have unique markers/ field-names, but just a multiple occurrence of "value_type" and "value", preceeding the 'name', respectively the 'value': see below.

{
    "software_version": "NRZ-2017-099", 
    "age":"78", 
    "sensordatavalues":[
        {"value_type":"SDS_P1","value":"4.43"},
        {"value_type":"SDS_P2","value":"3.80"},
        {"value_type":"temperature","value":"20.10"},
        {"value_type":"humidity","value":"44.50"},
        {"value_type":"samples","value":"614292"},
        {"value_type":"min_micro","value":"233"},
        {"value_type":"max_micro","value":"25951"},
        {"value_type":"signal","value":"-66"}
    ]
}

Experience as described above does not provide working solution.

Question: In this json-filelayout, how to directly extract the specific, individual values ( preferably by lua-script)?

[Or might XML-parsing provide an easier solution?]

Here is Python to read the JSON file and make it more convenient:

import json
import pprint

with open("/tmp/foo.json") as j:
    data = json.load(j)

for sdv in data.pop('sensordatavalues'):
    data[sdv['value_type']] = sdv['value']

pprint.pprint(data)

The results:

{'SDS_P1': '4.43',
 'SDS_P2': '3.80',
 'age': '78',
 'humidity': '44.50',
 'max_micro': '25951',
 'min_micro': '233',
 'samples': '614292',
 'signal': '-66',
 'software_version': 'NRZ-2017-099',
 'temperature': '20.10'}

You might want to have a look into filter functions.

Eg in your example json to get only the dict that contains the value for samples you could go by:

sample_sensordata = list(filter(lambda d: d["value_type"] == "samples", your_json_dict["sensordatavalues"]))

sample_value = sample_sensordata["value"]

To make a dictionary like Ned Batchelder said you could also go with a dict comprehension like this:

sensor_data_dict = {d['value_type']: d['value'] for d in a}

and then get the value you want just by sensor_data_dict['<ValueTypeYouAreLookingFor>']

A little bit late and I'm trying Anvil in which the previous answers didn't work. just for the curious people.

  resp = anvil.http.request("http://<ipaddress>/data.json", json=True)
  #print(resp) # prints json file
  tempdict = resp['sensordatavalues'][2].values() 
  humiddict = resp['sensordatavalues'][3].values()
  temperature = float(list(tempdict)[1])
  humidity = float(list(humiddict)[1])
  print(temperature)
  print(humidity)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM