Search code examples
phpjson-rpc

How to grab values to a variable from the result of API JSON-RPC response


{
    "jsonrpc": "2.0",
    "id": "123",
    "result": {
        "columns": [
            "Unit Number",
            "Tank Number",
            "Volume",
            "Volume Percent",
            "Description",
            "Capacity",
            "Status",
            "Last Updated"
        ],
        "values": [
            [
                null,
                null,
                "0",
                "0",
                "Tank 1",
                "50000",
                "N/A",
                "1970-01-01 09:30:00"
            ],
            [
                "3376",
                "1",
                "18490",
                "68.4815",
                "SmartFill 3376 Tank 1",
                "27000",
                "Offline",
                "2018-06-06 14:28:20"
            ]
        ]
    }
}

Solution

  • I believe you are looking for JSON.parse().

    The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

    const value = JSON.parse(`{ "jsonrpc": "2.0", "id": "123", "result": { "columns": [ "Unit Number", "Tank Number", "Volume", "Volume Percent", "Description", "Capacity", "Status", "Last Updated" ], "values": [ [ null, null, "0", "0", "Tank 1", "50000", "N/A", "1970-01-01 09:30:00" ], [ "3376", "1", "18490", "68.4815", "SmartFill 3376 Tank 1", "27000", "Offline", "2018-06-06 14:28:20" ] ] } }`);
    console.log(value.result.values);

    Hope this helps,