# Better way to parse JSON?

**URL:** https://discourse.nodered.org/t/better-way-to-parse-json/49579
**Category:** General
**Created:** [10 August 2021 18:32 UTC](https://discourse.nodered.org/t/better-way-to-parse-json/49579 "2021-08-10T18:32:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Xeniluom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xeniluom/32/46352_2.png) [@Xeniluom](https://discourse.nodered.org/u/Xeniluom)
#### Post date: [10 August 2021 18:32 UTC](https://discourse.nodered.org/t/better-way-to-parse-json/49579/1 "2021-08-10T18:32:15Z")

</div>

Hi,

in a previous post I ask how to find all global variable and it's works perfectly. ([List of all global](https://discourse.nodered.org/t/list-of-all-global/49567))

the JSON i receive is like this :

```auto
{
    "payload": [
        {
            "maison/bureau/temperature": "26.30"
        },
        {
            "maison/bureau/humidity": "57.00"
        },
        {
            "maison/bureau/consigne": 16
        }
    ],
    "topic": "uibuilder",
    "_msgid": "293c262c7209e491",
    "count": 3
}

```

Now, I need to use thoses variables in the uibuilder side.  
I make a function like this :

```auto
var humidite;
var temperature;

function variables(json) {
    for (i=0;i<json.count;i++){
        if (typeof json.payload[i]["maison/bureau/temperature"] === 'undefined'){}else{temperature = json.payload[i]["maison/bureau/temperature"]}
        if (typeof json.payload[i]["maison/bureau/humidity"] === 'undefined'){}else{humidite = json.payload[i]["maison/bureau/humidity"]}
        }
}

window.onload = function() {
    // Start up uibuilder - see the docs for the optional parameters
    uibuilder.start()

    // Listen for incoming messages from Node-RED
    uibuilder.onChange('msg', function(msg){
        console.info('[indexjs:uibuilder.onChange] msg received from Node-RED server:', msg)
variables(msg)
})
}

```

but I don't think it's the better way to do that.  
Anybody have a better method?

Thanks

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [10 August 2021 19:03 UTC](https://discourse.nodered.org/t/better-way-to-parse-json/49579/2 "2021-08-10T19:03:19Z")

</div>

a couple of things

1. you receive the msg in **uibuilder.onChange** and then you call **variables(msg)** and pass the whole msg **object** to it. Your function runs and then you try to loop through **json**.. but **json** is an object.  
You should have called **variables** with the msg.payload ... **variables(msg.payload)**  
Payload is an array and you can loop through it.

2. `for (i=0;i<json.count;i++)``count` ? shouldnt that be `json.length` ?

3. is a loop even necessary just to assign 2 variables to their values ?

```auto
var humidite;
var temperature;

function variables(json) {
        if (typeof json.payload[0]["maison/bureau/temperature"] !== 'undefined') {temperature = json.payload[0]["maison/bureau/temperature"]}
        if (typeof json.payload[1]["maison/bureau/humidity"] !== 'undefined') { humidite = json.payload[1]["maison/bureau/humidity"]}
      
}

```

ps. that why i recommended in your previous post to save the data in an object instead 😉  
because there are many points of failure, as nothing guaranties you that the array elements will arrive in the same order. Better send an object instead.

---

<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: [10 August 2021 20:19 UTC](https://discourse.nodered.org/t/better-way-to-parse-json/49579/3 "2021-08-10T20:19:03Z")

</div>

> [@Xeniluom](#):
>
> the JSON i receive is like this

It is important in IT to use the correct terms, to avoid confusion. That isn't JSON. JSON is always a string. It is a javascript object.

[This post](https://dev.to/desoga/javascript-object-vs-json-demystified-494j) should help to clarify the difference between Javascript objects and JSON.

---

<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: [9 October 2021 20:20 UTC](https://discourse.nodered.org/t/better-way-to-parse-json/49579/4 "2021-10-09T20:20:01Z")

</div>

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