Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Saving JSON to Google Sheet

46 views
Skip to first unread message

Jeremy Goff

unread,
Feb 23, 2025, 7:36:51 AMFeb 23
to Google Apps Script Community
I ask for some help and advice to save a JSON array to a sheet.

I fetch data from an API and parse it to JSON as shown below:

The count (3) will be the number of rows written to the sheet.

I want to write some of the items, e.g. "date", "totalActivePower", "temperature" as the column headings, into "count" rows.

I'm resonably OK with handling the sheet, but just can't seem to extract the data as above.

Help from the community would be most welcome

{
  "data": {
    "count": 3,
    "telemetries": [
      {
        "date": "2025-02-22 13:49:45",
        "totalActivePower": 1967,
        "dcVoltage": 366.812,
        "groundFaultResistance": 6000,
        "powerLimit": 100,
        "totalEnergy": 24116700,
        "temperature": 24.6502,
        "inverterMode": "MPPT",
        "operationMode": 0,
        "L1Data": {
          "acCurrent": 8.13281,
          "acVoltage": 242.984,
          "acFrequency": 50.0975,
          "apparentPower": 1972.5,
          "activePower": 1967,
          "reactivePower": 147.5,
          "cosPhi": 1
        }
      },
      {
        "date": "2025-02-22 13:54:45",
        "totalActivePower": 584,
        "dcVoltage": 369.312,
        "groundFaultResistance": 6000,
        "powerLimit": 100,
        "totalEnergy": 24116800,
        "temperature": 24.3047,
        "inverterMode": "MPPT",
        "operationMode": 0,
        "L1Data": {
          "acCurrent": 2.41406,
          "acVoltage": 244.406,
          "acFrequency": 50.1134,
          "apparentPower": 590,
          "activePower": 584,
          "reactivePower": 80,
          "cosPhi": 1
        }
      },
      {
        "date": "2025-02-22 13:59:45",
        "totalActivePower": 588,
        "dcVoltage": 369,
        "groundFaultResistance": 6000,
        "powerLimit": 100,
        "totalEnergy": 24116900,
        "temperature": 23.8055,
        "inverterMode": "MPPT",
        "operationMode": 0,
        "L1Data": {
          "acCurrent": 2.42969,
          "acVoltage": 244.641,
          "acFrequency": 49.9722,
          "apparentPower": 593,
          "activePower": 588,
          "reactivePower": -73,
          "cosPhi": 1
        }
      }
    ]
  }
}

Andrew Roberts

unread,
Feb 23, 2025, 7:59:46 AMFeb 23
to google-apps-sc...@googlegroups.com
It'll be something like this:

function getData() {
  const data = getJsonFromAPI() // One for you
  const numberOfRows = data.count
  const sheet = getSheet() // One for you
  for (let i = 0; i < numberOfRows; i++) {
    const input = data.telemetries[i]
    sheet.apprendRow([input.date, input.totalActivePower, input.temperature])
  }
}

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/49990e5b-1a3f-48e1-be40-aef97224c7e6n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages