# String to json array

**URL:** https://discourse.nodered.org/t/string-to-json-array/74206
**Category:** General
**Created:** [26 January 2023 09:19 UTC](https://discourse.nodered.org/t/string-to-json-array/74206 "2023-01-26T09:19:45Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![sahar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahar/32/86660_2.png) [@sahar](https://discourse.nodered.org/u/sahar)
#### Post date: [26 January 2023 09:19 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/1 "2023-01-26T09:19:45Z")

</div>

hi , I need help again at node red , I have this string at msg.payload (receive from data base as json)

```auto
"{"id":"2","sensor":"DHT11","value1":"26.20","location":"Office"}{"id":"3","sensor":"DHT11","value1":"26.10","location":"Office"}{"id":"4","sensor":"DHT11","value1":"25.60","location":"Office"}{"id":"5","sensor":"DHT11","value1":"25.50","location":"Office"}{"id":"6","sensor":"DHT11","value1":"25.50","location":"Office"}{"id":"7","sensor":"DHT11","value1":"25.40","location":"Office"}{"id":"8","sensor":"DHT11","value1":"25.40","location":"Office"}{"id":"9","sensor":"DHT11","value1":"25.40","location":"Office"}{"id":"10","sensor":"DHT11","value1":"25.30","location":"Office"}{"id":"11","sensor":"DHT11","value1":"25.20","location":"Office"}{"id":"12","sensor":"DHT11","value1":"25.30","location":"Office"}{"id":"13","sensor":"DHT11","value1":"25.10","location":"Office"}{"id":"14","sensor":"DHT11","value1":"25.10","location":"Office"}{"id":"15","sensor":"DHT11","value1":"25.10","location":"Office"}{"id":"16","sensor":"DHT11","value1":"25.10","location":"Office"}"

```

now i want show them at dashboard with table , for that i tried to parse them to jason format  
but i get : SyntaxError: Unexpected token { in JSON at position 64 node red  
php code for fetch is

```auto
 $sql = mysqli_query($conn , "SELECT `id`,`sensor`, `value1`,`location` FROM `sensor_data` WHERE `location`='Office' ");  
        
                 while($data = mysqli_fetch_row($sql)){ 

                    $id = $data[0];
                    $sensor = $data[1];
                    $value1 = $data[2];
                    $location= $data[3];
                    

                    $data = array('id' => $id, 'sensor' => $sensor, 'value1' => $value1 ,'location' => $location );    
                    $json = json_encode( $data );
                    echo $json;    

           } 

```

---

<div class="post-metadata">

### Author: ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)
#### Post date: [26 January 2023 09:30 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/2 "2023-01-26T09:30:27Z")

</div>

Your JSON string is not correct:  
What you get is an array of objects, so it must be enclosed by square brackets ( [...] ) and separated from each other with comma signs:

```auto
"[{"id":"2","sensor":"DHT11","value1":"26.20","location":"Office"},{"id":"3","sensor":"DHT11","value1":"26.10","location":"Office"},{"id":"4","sensor":"DHT11","value1":"25.60","location":"Office"},{"id":"5","sensor":"DHT11","value1":"25.50","location":"Office"},{"id":"6","sensor":"DHT11","value1":"25.50","location":"Office"},{"id":"7","sensor":"DHT11","value1":"25.40","location":"Office"},{"id":"8","sensor":"DHT11","value1":"25.40","location":"Office"},{"id":"9","sensor":"DHT11","value1":"25.40","location":"Office"},{"id":"10","sensor":"DHT11","value1":"25.30","location":"Office"},{"id":"11","sensor":"DHT11","value1":"25.20","location":"Office"},{"id":"12","sensor":"DHT11","value1":"25.30","location":"Office"},{"id":"13","sensor":"DHT11","value1":"25.10","location":"Office"},{"id":"14","sensor":"DHT11","value1":"25.10","location":"Office"},{"id":"15","sensor":"DHT11","value1":"25.10","location":"Office"},{"id":"16","sensor":"DHT11","value1":"25.10","location":"Office"}]"

```

---

<div class="post-metadata">

### Author: ![sahar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahar/32/86660_2.png) [@sahar](https://discourse.nodered.org/u/sahar)
#### Post date: [26 January 2023 09:44 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/3 "2023-01-26T09:44:23Z")

</div>

thank you , but how I can change it to that format ?

---

<div class="post-metadata">

### Author: ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)
#### Post date: [26 January 2023 09:50 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/4 "2023-01-26T09:50:17Z")

</div>

Is the string you posted exactly what you get from the database?

---

<div class="post-metadata">

### Author: ![sahar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahar/32/86660_2.png) [@sahar](https://discourse.nodered.org/u/sahar)
#### Post date: [26 January 2023 09:52 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/5 "2023-01-26T09:52:46Z")

</div>

yes , it is the exact string

---

<div class="post-metadata">

### Author: ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)
#### Post date: [26 January 2023 10:04 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/6 "2023-01-26T10:04:15Z")

</div>

Aahh, I see...  
The problem is in your PHP code:  
You echo the single lines from your query instead of the whole result at once:

Try this (hope that is correct, I didn't write PHP code for many years 😃 ):

```auto
$sql = mysqli_query($conn , "SELECT `id`,`sensor`, `value1`,`location` FROM `sensor_data` WHERE `location`='Office' ");  
        
                 $result = array();

                 while($data = mysqli_fetch_row($sql)){ 

                    $id = $data[0];
                    $sensor = $data[1];
                    $value1 = $data[2];
                    $location= $data[3];
                    

                    $data = array('id' => $id, 'sensor' => $sensor, 'value1' => $value1 ,'location' => $location );    
                    $json = json_encode( $data );
                    $result[] = $json;
                    

           }
           echo $result;

```

---

<div class="post-metadata">

### Author: ![sahar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahar/32/86660_2.png) [@sahar](https://discourse.nodered.org/u/sahar)
#### Post date: [26 January 2023 10:39 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/7 "2023-01-26T10:39:48Z")

</div>

thank you a lot , but it send me this :

```auto
msg.payload : string[5]

"Array"

```

---

<div class="post-metadata">

### Author: ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)
#### Post date: [26 January 2023 10:48 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/8 "2023-01-26T10:48:40Z")

</div>

> [@OliverWieland](#):
>
> `echo $result;`

```auto
echo json_encode($result);

```

---

<div class="post-metadata">

### Author: ![OliverWieland](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oliverwieland/32/74937_2.png) [@OliverWieland](https://discourse.nodered.org/u/OliverWieland)
#### Post date: [26 January 2023 10:53 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/9 "2023-01-26T10:53:49Z")

</div>

Sorry, more errors in my code...

```auto
$data = array('id' => $id, 'sensor' => $sensor, 'value1' => $value1 ,'location' => $location );    
// $json = json_encode( $data );
// $result[] = $json;
                    $result[] = $data;

```

---

<div class="post-metadata">

### Author: ![sahar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sahar/32/86660_2.png) [@sahar](https://discourse.nodered.org/u/sahar)
#### Post date: [26 January 2023 11:17 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/10 "2023-01-26T11:17:29Z")

</div>

now i can see table at dashboard , thank you alot

---

<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 February 2023 11:17 UTC](https://discourse.nodered.org/t/string-to-json-array/74206/11 "2023-02-09T11:17:46Z")

</div>

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