Need Help transforming API response

Need help transforming the following API Response to insert into a table:
ID, MeterID, datetime, reading).

I only need:

  • emigId
  • readings.importEnergy.value
  • readings.ts

JSON just isn't my thing (yet). Everything I try says data not in array or it returns everything. I could alter the table to store everything but given I don't need anything else it seems a waste.

Thanks for any help

{
  "emigId": "PV:001111",
  "meterSerial": "0007777777",
  "type": "PV",
  "description": "New PV Generation",
  "startDate": "20241006",
  "minIntervalS": "1800",
  "readings": [
    {
      "ts:": "2024-10-06T00:00:00.000000Z",
      "ts": "2024-10-06T00:00:00.000000Z",
      "importEnergy": {
        "value": 14773160,
        "unit": "Wh"
      },
      "importActivePower": {
        "value": -20,
        "unit": "W"
      },
      "apparentPower": {
        "value": 21474836,
        "unit": "VA"
      },
      "currentL1": {
        "value": 0.21,
        "unit": "A"
      },
      "currentL2": {
        "value": 0.192,
        "unit": "A"
      },
      "currentL3": {
        "value": 0.2,
        "unit": "A"
      },
      "mainsFrequency": {
        "value": 49.983,
        "unit": "Hz"
      },
      "powerFactor": {
        "value": -0.135,
        "unit": null
      },
      "reactivePower": {
        "value": 21474836,
        "unit": "VAR"
      },
      "voltageL1L2": {
        "value": 426.833,
        "unit": "V"
      },
      "voltageL2L3": {
        "value": 428.7,
        "unit": "V"
      },
      "voltageL1L3": {
        "value": 427.833,
        "unit": "V"
      },
      "voltageL1N": {
        "value": 246.733,
        "unit": "V"
      },
      "voltageL2N": {
        "value": 246.867,
        "unit": "V"
      },
      "voltageL3N": {
        "value": 247.383,
        "unit": "V"
      }
    },
    {
      "ts:": "2024-10-06T00:30:00.000000Z",
      "ts": "2024-10-06T00:30:00.000000Z",
      "importEnergy": {
        "value": 14773160,
        "unit": "Wh"
      },
      "importActivePower": {
        "value": -20,
        "unit": "W"
      },
      "apparentPower": {
        "value": 21474836,
        "unit": "VA"
      },
      "currentL1": {
        "value": 0.207,
        "unit": "A"
      },
      "currentL2": {
        "value": 0.193,
        "unit": "A"
      },
      "currentL3": {
        "value": 0.2,
        "unit": "A"
      },
      "mainsFrequency": {
        "value": 49.992,
        "unit": "Hz"
      },
      "powerFactor": {
        "value": -0.137,
        "unit": null
      },
      "reactivePower": {
        "value": 21474836,
        "unit": "VAR"
      },
      "voltageL1L2": {
        "value": 425.167,
        "unit": "V"
      },
      "voltageL2L3": {
        "value": 427.167,
        "unit": "V"
      },
      "voltageL1L3": {
        "value": 425.65,
        "unit": "V"
      },
      "voltageL1N": {
        "value": 245.6,
        "unit": "V"
      },
      "voltageL2N": {
        "value": 246.033,
        "unit": "V"
      },
      "voltageL3N": {
        "value": 246.283,
        "unit": "V"
      }
    },
  ],
  "startTime": "000000.000000"
}

Hi @Footsore, You can easily extract the keys you need using the code below.

const result = {
  emigId: data.emigId,
  ts_1: data.readings[0].ts,
  importEnergyValue_1: data.readings[0].importEnergy.value,
  ts_2: data.readings[1].ts,
  importEnergyValue_2: data.readings[1].importEnergy.value
};

return result;
1 Like

That might work, but I could (will) have more lines as times progresses. I'll likely do daily download so at least 48. And if it falls over it could be more lines. The API request picks the last date I have and asks for data from that point forward. I have one with 174 in at the moment but put a truncated version up.

const result = data.readings.map(reading => ({
  ID: data.emigId,
  MeterID: data.meterSerial,
  datetime: reading.ts,
  reading: reading.importEnergy.value
}));

return result;
3 Likes

that's the cookie! Thank you. I suspect I have a lot to learn.

1 Like