# Parsing JSON and Return Last Value

**URL:** <https://discourse.nodered.org/t/parsing-json-and-return-last-value/51367>\
**Category:** General\
**Tags:** weather\
**Created:** [22 September 2021 00:25 UTC](https://discourse.nodered.org/t/parsing-json-and-return-last-value/51367 "2021-09-22T00:25:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [22 September 2021 00:25 UTC](https://discourse.nodered.org/t/parsing-json-and-return-last-value/51367/1 "2021-09-22T00:25:52Z")

</div>

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](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:

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [22 September 2021 01:05 UTC](https://discourse.nodered.org/t/parsing-json-and-return-last-value/51367/2 "2021-09-22T01:05:39Z")

</div>

> [@dandspach](#):
>
> I noticed the ' [{ '

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.

---

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [22 September 2021 01:15 UTC](https://discourse.nodered.org/t/parsing-json-and-return-last-value/51367/3 "2021-09-22T01:15:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 September 2021 06:32 UTC](https://discourse.nodered.org/t/parsing-json-and-return-last-value/51367/4 "2021-09-22T06:32:56Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![dandspach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dandspach/32/48438_2.png) [@dandspach](https://discourse.nodered.org/u/dandspach)\
**Post date:** [22 September 2021 14:57 UTC](https://discourse.nodered.org/t/parsing-json-and-return-last-value/51367/5 "2021-09-22T14:57:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 October 2021 14:58 UTC](https://discourse.nodered.org/t/parsing-json-and-return-last-value/51367/6 "2021-10-06T14:58:03Z")

</div>

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