Home > database >  Query Nested JSON Data
Query Nested JSON Data

Time:02-04

I am trying to query using the code below to get the value etoday but it does not return a result.

  Result = json.loads(json_string)
  # Next Line works perfect
  print("The value of msg", "msg", "is: ", Result["msg"])
  #Next Line does not (I have a few variations here that I tried
  print(Result['data']['page']['records']['etoday'])
  print(Result["data"][0]["page"][0]["etoday"])
  print(Result['page']['records'][0]['etoday'])
  Print("The value of", "etoday", "is: ", Result["etoday"])

File I am querying

{
  "success": true,
  "code": "0",
  "msg": "success",
  "data": {
    "inverterStatusVo": {
      "all": 1,
      "normal": 1,
      "fault": 0,
      "offline": 0,
      "mppt": 0
    },
    "page": {
      "records": [
        {
          "id": "zzz",
          "sn": "xxx",
          "collectorSn": "vvv",
          "userId": "ttt",
          "productModel": "3105",
          "nationalStandards": "68",
          "inverterSoftwareVersion": "3d0037",
          "dcInputType": 2,
          "acOutputType": 0,
          "stationType": 0,
          "stationId": "1298491919448828419",
          "rs485ComAddr": "101",
          "simFlowState": -1,
          "power": 6.000,
          "powerStr": "kW",
          "pac": 0.001,
          "pac1": 0,
          "pacStr": "kW",
          "state": 1,
          "stateExceptionFlag": 0,
          "fullHour": 1.82,
          "totalFullHour": 191.83,
          "maxDcBusTime": "1675460767377",
          "maxUac": 251.5,
          "maxUacTime": "1664022228160",
          "maxUpv": 366.3,
          "maxUpvTime": "1663309960961",
          "timeZone": 0.00,
          "timeZoneStr": "(UTC 00:00)",
          "timeZoneName": "(UTC 00:00)Europe/Dublin",
          "dataTimestamp": "1675460767377",
          "dataTimestampStr": "2023-02-03 21:46:07 (UTC 00:00)",
          "fisTime": "1663189116371",
          "inverterMeterModel": 5,
          "updateShelfBeginTime": 1671292800000,
          "updateShelfEndTime": 1829059200000,
          "updateShelfEndTimeStr": "2027-12-18",
          "updateShelfTime": "5",
          "collectorId": "1306858901387600551",
          "dispersionRate": 0.0,
          "currentState": "3",
          "pow1": 0.0,
          "pow2": 0.0,
          "pow3": 0.0,
          "pow4": 0.0,
          "pow5": 0.0,
          "pow6": 0.0,
          "pow7": 0.0,
          "pow8": 0.0,
          "pow9": 0.0,
          "pow10": 0.0,
          "pow11": 0.0,
          "pow12": 0.0,
          "pow13": 0.0,
          "pow14": 0.0,
          "pow15": 0.0,
          "pow16": 0.0,
          "pow17": 0.0,
          "pow18": 0.0,
          "pow19": 0.0,
          "pow20": 0.0,
          "pow21": 0.0,
          "pow22": 0.0,
          "pow23": 0.0,
          "pow24": 0.0,
          "pow25": 0.0,
          "pow26": 0.0,
          "pow27": 0.0,
          "pow28": 0.0,
          "pow29": 0.0,
          "pow30": 0.0,
          "pow31": 0.0,
          "pow32": 0.0,
          "gridPurchasedTodayEnergy": 3.800,
          "gridPurchasedTodayEnergyStr": "kWh",
          "gridSellTodayEnergy": 2.400,
          "gridSellTodayEnergyStr": "kWh",
          "psumCalPec": "1",
          "batteryPower": 0.099,
          "batteryPowerStr": "kW",
          "batteryPowerPec": "1",
          "batteryCapacitySoc": 20.000,
          "parallelStatus": 0,
          "parallelAddr": 0,
          "parallelPhase": 0,
          "parallelBattery": 0,
          "batteryTodayChargeEnergy": 4.800,
          "batteryTodayChargeEnergyStr": "kWh",
          "batteryTotalChargeEnergy": 449.000,
          "batteryTotalChargeEnergyStr": "kWh",
          "batteryTodayDischargeEnergy": 5.500,
          "batteryTodayDischargeEnergyStr": "kWh",
          "batteryTotalDischargeEnergy": 627.000,
          "batteryTotalDischargeEnergyStr": "kWh",
          "bypassLoadPower": 0.000,
          "bypassLoadPowerStr": "kW",
          "backupTodayEnergy": 0.000,
          "backupTodayEnergyStr": "kWh",
          "backupTotalEnergy": 0.000,
          "backupTotalEnergyStr": "kWh",
          "etotal": 1.153,
          "etoday": 10.900,
          "psum": -1.756,
          "psumCal": -1.756,
          "etotal1": 1153.000,
          "etoday1": 10.900000,
          "offlineLongStr": "--",
          "etotalStr": "MWh",
          "etodayStr": "kWh",
          "psumStr": "kW",
          "psumCalStr": "kW"
        }
      ],
      "total": 1,
      "size": 20,
      "current": 1,
      "orders": [
        
      ],
      "optimizeCountSql": false,
      "searchCount": true,
      "pages": 1
    },
    "mpptSwitch": 0
  }
}

CodePudding user response:

The records key is the only array in your path, that's the only one where you need to select with an index as below:

Result['data']['page']['records'][0]['etoday']

CodePudding user response:

The issue might be that the key etoday is not present at the location you are trying to access it from. Try using the following code to see if the key exists in the JSON object:

if 'etoday' in Result['data']['page']['records']: print("The value of etoday is: ", Result['data']['page']['records']['etoday']) else: print("The key 'etoday' was not found in the Result['data']['page']['records'] dictionary.")

  • Related