Trying to obtain data from nested string

Forgive me if I am in the wrong section, I am blind and having a hard time navigating the site.
I am trying to obtain data from a json file using the infinity datasource. I can get top level information correctly but am unsure how to obtain sublevel data. Example is below. I am trying to extract pump>battery>status as a string. I hope im clear enough on my intent. I don’t work with json enough to really gather all that im doing, at least not sublevel data. (?)

{
    "device": "string",
    "created_at": "string",
    "openaps": "string",
    "loop": "string",
    "pump": {
      "clock": "string",
      "battery": {
        "status": "string",
        "voltage": 0
      },

Thank you for your time and greatly appreciate any help you may be able to provide.

Welcome

Try it using UQL

parse-json
| scope "pump.battery.status"

Thank you so much for your reply, I think im doing something wrong though. I tried exactly like you had typed out and I was getting nothing. I am realized I didn’t have data for pump battery yet, but I do for uploader battery, in the case you wrote would be uploader.battery. I am attaching the json file with a small set of logs so maybe you can understand better what data I have to work with. There are 3 json files that have a lot more info but for sake of understanding I am attaching the most basic one. Also attaching what the api documents have outlined. These werent made for grafana which is why I am having trouble :slight_smile:

Outlined json file for devicestatus.json

[
  {
    "device": "string",
    "created_at": "string",
    "openaps": "string",
    "loop": "string",
    "pump": {
      "clock": "string",
      "battery": {
        "status": "string",
        "voltage": 0
      },
      "reservoir": 0,
      "status": {
        "status": "string",
        "bolusing": true,
        "suspended": true,
        "timestamp": "string"
      }
    },
    "uploader": {
      "batteryVoltage": 0,
      "battery": 0
    },
    "xdripjs": {
      "state": 0,
      "stateString": "string",
      "stateStringShort": "string",
      "txId": "string",
      "txStatus": 0,
      "txStatusString": "string",
      "txStatusStringShort": "string",
      "txActivation": 0,
      "mode": "string",
      "timestamp": 0,
      "rssi": 0,
      "unfiltered": 0,
      "filtered": 0,
      "noise": 0,
      "noiseString": 0,
      "slope": 0,
      "intercept": 0,
      "calType": "string",
      "lastCalibrationDate": 0,
      "sessionStart": 0,
      "batteryTimestamp": 0,
      "voltagea": 0,
      "voltageb": 0,
      "temperature": 0,
      "resistance": 0
    }
  }
]

Additinally the current small recorded data in the json file i need to extract fromL

[{"_id":"63b4a35a74b87e8ad5fcab7d","device":"samsung SM-F926U","uploader":{"battery":13,"type":"PHONE"},"created_at":"2023-01-03T21:51:22.756Z","utcOffset":-480},{"_id":"63b4a22e74b87e07dbfcab7c","device":"samsung SM-F926U","uploader":{"battery":11,"type":"PHONE"},"created_at":"2023-01-03T21:46:22.725Z","utcOffset":-480},{"_id":"63b4a10474b87e641ffcab7a","device":"samsung SM-F926U","uploader":{"battery":11,"type":"PHONE"},"created_at":"2023-01-03T21:41:24.737Z","utcOffset":-480},{"_id":"63b49fdc74b87ec81efcab79","device":"samsung SM-F926U","uploader":{"battery":12,"type":"PHONE"},"created_at":"2023-01-03T21:36:28.690Z","utcOffset":-480},{"_id":"63b49ead74b87e8645fcab78","device":"samsung SM-F926U","uploader":{"battery":13,"type":"PHONE"},"created_at":"2023-01-03T21:31:25.371Z","utcOffset":-480},{"_id":"63b49d8474b87e8736fcab77","device":"samsung SM-F926U","uploader":{"battery":14,"type":"PHONE"},"created_at":"2023-01-03T21:26:28.911Z","utcOffset":-480},{"_id":"63b49d7e74b87e43b0fcab76","device":"samsung SM-F926U","uploader":{"battery":14,"type":"PHONE"},"created_at":"2023-01-03T21:26:22.537Z","utcOffset":-480},{"_id":"63b49d3a74b87e1dbbfcab75","device":"samsung SM-F926U","uploader":{"battery":14,"type":"PHONE"},"created_at":"2023-01-03T21:25:14.123Z","utcOffset":-480}]

Once again,. thank you for all your help :slight_smile:

To do crm enrichment from a nested JSON object like the one you provided, you can use a dot notation approach. In this case, to extract the “status” property within the “battery” object, you would use the following code:

data["pump"]["battery"]["status"]

This code assumes that you have already loaded the JSON data into a variable called “data”. If you haven’t done that yet, you can do so using the json module in Python:

import json

data = json.loads(‘{“device”: “string”, “created_at”: “string”, “openaps”: “string”, “loop”: “string”, “pump”: {“clock”: “string”, “battery”: {“status”: “string”, “voltage”: 0}}}’)

Once you have loaded the JSON data into a variable, you can use the dot notation to access any property within the object. For example, to get the value of the “device” property, you would use the following code:

data["device"]

1 Like