Parsing JSON and Return Last Value

I'm brand new to Node Red, please be patient with my question. I've seached forums, watched Youtube and tried my best to answer this for myself, but I still can't make it happen...so here's my issues.

I want to get the latest value from the following website (https://services.swpc.noaa.gov/json/boulder_k_index_1m.json)

I am using what appears to be 'nested' json data from the following . I noticed the ' [{ ' which is unlike the tutorials I've been through. I'm stuck because I have to run it through two JSON parsers to even get close to a clean debugging output.

I would also like to only return the last "kp_index" value. Any help would be appreciated.

My Node Red output code:

[
    {
        "id": "61bca993f8e4f577",
        "type": "tab",
        "label": "Space Weather",
        "disabled": false,
        "info": ""
    },
    {
        "id": "23fa9e5c20f7fab1",
        "type": "inject",
        "z": "61bca993f8e4f577",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 60,
        "wires": [
            [
                "00fcc86b1508649b"
            ]
        ]
    },
    {
        "id": "00fcc86b1508649b",
        "type": "http request",
        "z": "61bca993f8e4f577",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://services.swpc.noaa.gov/json/boulder_k_index_1m.json",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 250,
        "y": 140,
        "wires": [
            [
                "26baeac30c05e8dd"
            ]
        ]
    },
    {
        "id": "26baeac30c05e8dd",
        "type": "json",
        "z": "61bca993f8e4f577",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 300,
        "y": 240,
        "wires": [
            [
                "d8db3d11efdb2225"
            ]
        ]
    },
    {
        "id": "7475d16e4676eef7",
        "type": "debug",
        "z": "61bca993f8e4f577",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 650,
        "y": 320,
        "wires": []
    },
    {
        "id": "d8db3d11efdb2225",
        "type": "json",
        "z": "61bca993f8e4f577",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 400,
        "y": 320,
        "wires": [
            [
                "551d794917251537"
            ]
        ]
    },
    {
        "id": "551d794917251537",
        "type": "function",
        "z": "61bca993f8e4f577",
        "name": "",
        "func": "\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 530,
        "y": 400,
        "wires": [
            [
                "7475d16e4676eef7"
            ]
        ]
    }
]

That says it is an array of objects. If you put a debug node on the output of the first json node you will see the array of objects.

Got it. That makes sense.

A new object is created each minute of the day. If it were static, I could just pull a specific object, but they are continually added throughout the day. Any further ideas?

Use the length property of the array to target the last element.
eg.

[{"id":"23fa9e5c20f7fab1","type":"inject","z":"61bca993f8e4f577","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":60,"wires":[["00fcc86b1508649b"]]},{"id":"00fcc86b1508649b","type":"http request","z":"61bca993f8e4f577","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://services.swpc.noaa.gov/json/boulder_k_index_1m.json","tls":"","persist":false,"proxy":"","authType":"","x":250,"y":140,"wires":[["551d794917251537","7475d16e4676eef7"]]},{"id":"551d794917251537","type":"function","z":"61bca993f8e4f577","name":"","func":"msg.payload = msg.payload[msg.payload.length-1].k_index\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":300,"wires":[["7475d16e4676eef7"]]},{"id":"7475d16e4676eef7","type":"debug","z":"61bca993f8e4f577","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":320,"wires":[]}]

Thank you both for your assistance. It works perfect now...

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.