# Formatting JSON Array to node-red

**URL:** <https://discourse.nodered.org/t/formatting-json-array-to-node-red/17857>\
**Category:** General\
**Created:** [12 November 2019 19:29 UTC](https://discourse.nodered.org/t/formatting-json-array-to-node-red/17857 "2019-11-12T19:29:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![redlop](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@redlop](https://discourse.nodered.org/u/redlop)\
**Post date:** [12 November 2019 19:29 UTC](https://discourse.nodered.org/t/formatting-json-array-to-node-red/17857/1 "2019-11-12T19:29:25Z")

</div>

Hi, I have an ESP8266 program that prints the networks scanned and places them in a JSON Array. I'm trying to send it to via mqtt and node-red. I've also converted it into a String in arduino but when I connect a text node it prints the array with braces and quotations. How do I program node-red into printing only the SSID and RSSI values, and a new line for every network found.

```auto
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "Tenda_FE2038";
const char* password = "huertas032793";
const char* mqtt_server = "192.168.1.2";

WiFiClient espClient;
PubSubClient client(espClient);

void callback(String topic, byte* message, unsigned int length) {
 // Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
  
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  
      // Subscribe or resubscribe to a topic
      // You can subscribe to more topics (to control more LEDs in this example)
      client.subscribe("rssi/test");
      client.subscribe("name/test");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

DynamicJsonDocument getAccessPointsAsDynamicJson() { 
  DynamicJsonDocument doc(2560); 
  JsonArray array = doc.createNestedArray("Networks: "); 
  Serial.println("Start scanning"); 
  int n = WiFi.scanNetworks();
  Serial.print(n); 
  Serial.println(" networks found"); 
  for (int i = 0; i < n; ++i)
  { JsonObject 
  nested = array.createNestedObject(); 
  nested["ssid"] = WiFi.SSID(i); 
 // nested["bssid"] = WiFi.BSSID(i); 
  nested["rssi"] = WiFi.RSSI(i);
 // nested["channel"] = WiFi.channel(i); 
  } 
  return doc; 
  }

void setSerial() 
{ 
  Serial.begin(115200);

while (!Serial) continue;
}

void setup() 
{ 
  
  setSerial();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

}

void loop()
{ 

    if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client");
    
 DynamicJsonDocument doc = getAccessPointsAsDynamicJson(); 
const char* first_found_ssid = doc["ap"][0]["ssid"]; 
const int first_found_rssi = doc["ap"][0]["rssi"]; 
const char* second_found_ssid = doc["ap"][1]["ssid"]; 
//Serial.println(first_found_ssid);
//Serial.println(first_found_rssi); 
//Serial.println(second_found_ssid); 
delay(1000);

String myOutput; serializeJson(doc, myOutput);
char myOutput_char[200];
myOutput.toCharArray(myOutput_char, 200);
//Serial.println(myOutput);
 client.publish("name/test",myOutput_char);

}

```

```auto
[{"id":"889428da.48e2f8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"6b64ab7b.cd1ac4","type":"json","z":"889428da.48e2f8","name":"","property":"payload","action":"obj","pretty":true,"x":530,"y":300,"wires":[["301f0300.9c21fe"]]},{"id":"249daef3.964e22","type":"mqtt in","z":"889428da.48e2f8","name":"","topic":"name/test","qos":"0","datatype":"auto","broker":"6669e693.9f8508","x":360,"y":360,"wires":[["aaf0037d.3a5a7","6b64ab7b.cd1ac4"]]},{"id":"301f0300.9c21fe","type":"ui_text","z":"889428da.48e2f8","group":"13637635.f64a3a","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"col-center","x":690,"y":260,"wires":[]},{"id":"aaf0037d.3a5a7","type":"debug","z":"889428da.48e2f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":750,"y":420,"wires":[]},{"id":"6669e693.9f8508","type":"mqtt-broker","z":"","name":"mosquitto","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"13637635.f64a3a","type":"ui_group","z":"","name":"rssi","tab":"a9b73d04.c94c9","disp":true,"width":"6","collapse":false},{"id":"a9b73d04.c94c9","type":"ui_tab","z":"","name":"Home","icon":"mic","disabled":false,"hidden":false}]

```

this is the flow im working on

![payload](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fc797fdda4158321ba72ccd0af74d31ac7eb6e48.png)

this is what the output looks like in text node

---

<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:** [12 November 2019 20:25 UTC](https://discourse.nodered.org/t/formatting-json-array-to-node-red/17857/2 "2019-11-12T20:25:08Z")

</div>

Try selecting Parsed Json Object in the Output field of the MQTT node. Feed that into a debug node and make sure that it now shows as a javascript object. Then read the node red docs page on Working With Messages which will help you find how to extract the data you want.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [12 November 2019 20:58 UTC](https://discourse.nodered.org/t/formatting-json-array-to-node-red/17857/3 "2019-11-12T20:58:50Z")

</div>

Looks like there is already a `json` node in the flow -- but the `ui_text` node is converting the entire `msg.payload` back to a string for display... For the text format, try referencing just the field you want to display (SSID, RSSI, etc).

One caveat -- you are using the string `"Networks: "` as the property name, which will be hard to work with in Javascript. I would change that to just the plain word `Networks` without any punctuation or spaces. That way your `ui_text` format could be `{{msg.payload.Networks[0].ssid}}` for example.

I do not think that the simple text node will display an array of strings -- for that you might need a bit of Angular syntax embedded in a `ui_template` node, or maybe look at one of the html table nodes.

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [12 November 2019 21:14 UTC](https://discourse.nodered.org/t/formatting-json-array-to-node-red/17857/4 "2019-11-12T21:14:59Z")

</div>

As I wrote before, take a look at the ui-list or ui-table dashboard nodes (installable via the palette menu) You still have to do some conversions but this could be done with a function node or other node red standard nodes

---

<div class="post-metadata">

**Author:** ![redlop](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@redlop](https://discourse.nodered.org/u/redlop)\
**Post date:** [12 November 2019 22:20 UTC](https://discourse.nodered.org/t/formatting-json-array-to-node-red/17857/5 "2019-11-12T22:20:15Z")

</div>

Thank you all for you suggestions. I have succesfully edited it via template node with this code

```auto
<ul>
    <li ng-repeat="x in msg.payload.Networks">
        <span>{{x.ssid}}: {{x.rssi}}</span>
    </li>
</ul>

```

im passing the json string through a json node as a javascript object.

However i've run into a weird problem. My ESP8266 now doesn't send data through mqtt. It worked for about 10mins then stopped sending the data. I've tried connecting the device to a different router, and even uploading the code to a new one. But the problem persist. Is this a problem with node-red?
