# How do I extract values from a JSON string?

**URL:** https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521
**Category:** General
**Tags:** function-node
**Created:** [30 June 2022 01:45 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521 "2022-06-30T01:45:41Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![amit\_meld](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit_meld/32/79510_2.png) [@amit\_meld](https://discourse.nodered.org/u/amit_meld)
#### Post date: [30 June 2022 01:45 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/1 "2022-06-30T01:45:41Z")

</div>

Hello,

I am trying to push data to a data platform from a people counting sensor.  
I am receiving below JSON response from the API. I want to push Incount and outcount seperately to the data platform pool. I have set name as dataset name and incount, outcount will be two data streams inside that dataset. Could you please help me in the Function node scripting? The split node splits all below rows seperately in 'payload'.

Channel.0.LineIndex=1  
Channel.0.LineIndex.1.Name=Privacybooth  
Channel.0.LineIndex.1.InCount=12  
Channel.0.LineIndex.1.OutCount=10  
Channel.0.LineIndex=2  
Channel.0.LineIndex.2.Name=EntryDoor  
Channel.0.LineIndex.2.InCount=4  
Channel.0.LineIndex.2.OutCount=3

Kind regards,  
Amit

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [30 June 2022 02:45 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/2 "2022-06-30T02:45:44Z")

</div>

Can you show us how you are gettng this into NR ?

I would first try and feed it into the JSON Node and have it output a Javascript object and see what you get

- put a debug node on the output so we can see what it looks like but it will probably be something like

msg.payload.Channel.0.LineIndex etc etc which you can then refernce

Craig

---

<div class="post-metadata">

### Author: ![amit\_meld](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit_meld/32/79510_2.png) [@amit\_meld](https://discourse.nodered.org/u/amit_meld)
#### Post date: [30 June 2022 03:48 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/4 "2022-06-30T03:48:29Z")

</div>

I am pulling the data in by using HTTP node and API URL. 'Return a parsed JSON object' is turned ON in HTTP node. I have added a split node to get the payload seperate. I want Incount and outcount values as msg.payload as final upload for PrivacyBooth seperate and EntryDoor seperate.

Please see below the attached image for reference.

Thank your for your quick reply.

 ![Screen Shot 2022-06-30 at 1.47.55 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/4/8461dc99e9606f78b32af62008931938f8d6755a.png)

Kind regards,  
Amit

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [30 June 2022 04:15 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/5 "2022-06-30T04:15:51Z")

</div>

Nope thats not right - in the dedbug window we should be seeeing actual objects for each incoming message

You want them as a single object if possible because then you can access each of the object properties to do what you want.

Show us a screen grab of the http node and the json node

Change the debug node to complete message object

Craig

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [30 June 2022 04:23 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/6 "2022-06-30T04:23:22Z")

</div>

WHen you have a javascript object in a message you can reference any field quite easily using a variety of nodes - the change and switch nodes for instance

Here for instance is a weather object my provider returns to me

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5b512b13167ca9ee040be0d80b5a8443b7c081a2.png)

And i can reference any of its properties like this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/6540969b380f09ceca9edff45d1cc22e741d5978.png)

Craig

---

<div class="post-metadata">

### Author: ![amit\_meld](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit_meld/32/79510_2.png) [@amit\_meld](https://discourse.nodered.org/u/amit_meld)
#### Post date: [30 June 2022 04:24 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/7 "2022-06-30T04:24:21Z")

</div>

This is how it looks like. API gives me a string always.

 ![Screen Shot 2022-06-30 at 2.19.24 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1ad5bd06918588c41580a758478d55ddfc4b0977.png)  
 ![Screen Shot 2022-06-30 at 2.18.54 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9a9c4b379926284499b32f2ff413768ccb46de70.png)

---

<div class="post-metadata">

### Author: ![amit\_meld](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit_meld/32/79510_2.png) [@amit\_meld](https://discourse.nodered.org/u/amit_meld)
#### Post date: [30 June 2022 04:30 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/8 "2022-06-30T04:30:28Z")

</div>

And when I try to change the JSON node Action, debug throws an error saying 'Unexpected token C in JSON at position 0'

 ![Screen Shot 2022-06-30 at 2.27.01 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/dfea1f91d3eb14f836a7b9355d74fe998a42e603.png)

I am unable to get the payload in the format you have stated :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5b512b13167ca9ee040be0d80b5a8443b7c081a2.png)

I have done another flow in the javascript object format where I have successfully used function node for scripting and push the data to dataset. But with the payload I am receiving, I am not sure how do I retrieve the data from the string separately.

-Amit

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [30 June 2022 04:48 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/9 "2022-06-30T04:48:36Z")

</div>

This is where a function node is your friend, where you can get down and dirty with JavaScript to parse or extract values from given payload properties. Which object property is the one in question?

Access the array via a typical subscript, msg.payload[0], msg.payload[1], etc. Then var max=msg.payload[0].air\_temperature\_maximum; in a functional node, for example.

---

<div class="post-metadata">

### Author: ![amit\_meld](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit_meld/32/79510_2.png) [@amit\_meld](https://discourse.nodered.org/u/amit_meld)
#### Post date: [30 June 2022 05:47 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/10 "2022-06-30T05:47:33Z")

</div>

No Matter if I use JSON node or use 'Return Parsed JSON object' in HTTP node. I receive below below message as output in String format-

![Screen Shot 2022-06-30 at 3.42.33 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/792dc4742b754713da3d56d175c0b69528a85d0b.png)

Here, if I copy the path, it is always 'Payload' and I can not select individual object's path as 'Payload.channel.0.LineIndex.1' . This is where I am struggling.

Do I need to script in such a way that I reach the each character from the string in a loop one by one and extract the values I need? How do I do that? I request you to help me with a sample or guide.

Kind regards,  
Amit

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [30 June 2022 07:40 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/11 "2022-06-30T07:40:18Z")

</div>

That is because it is not a JSON string, it is just a string.

---

<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: [30 June 2022 08:37 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/12 "2022-06-30T08:37:51Z")

</div>

If the `http request` is returning a parsed json object, you don’t need the `json` node. If you add a `debug` node (set to display the complete msg object) to the output of the `http request` node, you will see if it is an object or not.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [1 July 2022 01:12 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/13 "2022-07-01T01:12:01Z")

</div>

Ok just so we are all on the same page can you do the following

1. Inject node
2. htt request node with apu etc and return to a parsed JSON object
3. Debug node - set to show complete message object

No other split, function, JSON nodes

And then show us the debug output window please

Craig

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [1 July 2022 01:25 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/14 "2022-07-01T01:25:49Z")

</div>

> [@amit\_meld](#):
>
> No Matter if I use JSON node or use 'Return Parsed JSON object' in HTTP node. I receive below below message as output in String format-
> 
> ![Screen Shot 2022-06-30 at 3.42.33 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/792dc4742b754713da3d56d175c0b69528a85d0b.png)
> 
> Here, if I copy the path, it is always 'Payload' and I can not select individual object's path as 'Payload.channel.0.LineIndex.1' . This is where I am struggling.
> 
> Do I need to script in such a way that I reach the each character from the string in a loop one by one and extract the values I need? How do I do that? I request you to help me with a sample or guide.

If after you do the test that i listed above can you also do the following so we get a better idea of what that string is - it appears that it is a string with a (NL) new line to delimit each of the values

To make sure this is the case can you install the following node

node-red-contrib-primitives

And then feed the output of the http node into the String SPlit node that it installs

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe6df0b140fced9ea069f0b71d3a45f65ac2c762.png)

And set that to

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a829219351a593cfbb10043ea249b55b7748bbbf.png)

And then Run - refresh the context data and show us the flow context

Craig

---

<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: [1 July 2022 08:20 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/15 "2022-07-01T08:20:35Z")

</div>

As others have said the string is not a JSON string, so you will need to parse the string.

you have not said how you wish the output to look so i have parsed it to an array of objects in my example.  
e.g.

```auto
[{"id":"f6332b85.359ec8","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":100,"wires":[["cd12fe0d.b624d8"]]},{"id":"cd12fe0d.b624d8","type":"template","z":"bf9e1e33.030598","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"Channel.0.LineIndex=1\nChannel.0.LineIndex.1.Name=Privacybooth\nChannel.0.LineIndex.1.InCount=12\nChannel.0.LineIndex.1.OutCount=10\nChannel.0.LineIndex=2\nChannel.0.LineIndex.2.Name=EntryDoor\nChannel.0.LineIndex.2.InCount=4\nChannel.0.LineIndex.2.OutCount=3","output":"str","x":270,"y":100,"wires":[["dd838b61.413be","efaa07a0.5dd0e8"]]},{"id":"dd838b61.413be","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload = msg.payload.split(\"\\n\");\nmsg.payload = msg.payload.reduce((acc, str) => {\n let parts = str.split(/\\.|=/);\n if(parts.length <= 4){\n acc[parts[3]-1] = {[parts[2]]:Number(parts[3]), [parts[0]]: Number(parts[1])};\n }else{\n acc[parts[3]-1][parts[4]] = isNaN(parseFloat(parts[5])) ? parts[5] : parseFloat(parts[5]);\n }\n return acc;\n},[])\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":100,"wires":[["9efd2c72.cb6e88"]]},{"id":"efaa07a0.5dd0e8","type":"debug","z":"bf9e1e33.030598","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":460,"y":40,"wires":[]},{"id":"9efd2c72.cb6e88","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":100,"wires":[]}]

```

```auto
msg.payload = msg.payload.split("\n");
msg.payload = msg.payload.reduce((acc, str) => {
    let parts = str.split(/\.|=/);
    if(parts.length <= 4){
        acc[parts[3]-1] = {[parts[2]]:Number(parts[3]), [parts[0]]: Number(parts[1])};
    }else{
        acc[parts[3]-1][parts[4]] = isNaN(parseFloat(parts[5])) ? parts[5] : parseFloat(parts[5]);
    }
    return acc;
},[])
return msg;

```

output

```auto
[
    {
        "LineIndex": 1,
        "Channel": 0,
        "Name": "Privacybooth",
        "InCount": 12,
        "OutCount": 10
    },
    {
        "LineIndex": 2,
        "Channel": 0,
        "Name": "EntryDoor",
        "InCount": 4,
        "OutCount": 3
    }
]

```

---

<div class="post-metadata">

### Author: ![amit\_meld](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amit_meld/32/79510_2.png) [@amit\_meld](https://discourse.nodered.org/u/amit_meld)
#### Post date: [5 July 2022 02:09 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/16 "2022-07-05T02:09:45Z")

</div>

Thank you so much everyone for helping me resolve this task ! I was out of station for the weekend and couldn't reply earlier. Yes, my bad, it is just a String and not 'JSON string'. I have accepted E1cid's post as solution, the flow works as expected. I am revisiting the community as I get to learn more from everyone and hopefully in future I could contribute.

---

<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: [19 July 2022 02:10 UTC](https://discourse.nodered.org/t/how-do-i-extract-values-from-a-json-string/64521/17 "2022-07-19T02:10:13Z")

</div>

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