0
votes

I want to extract json part in a message with node red

I receive the message in node red but I can't extract the data exemple of a message:

POST / HTTP/1.1

Host: xxx.xx.xxx.5:9000

User-Agent: libcurl-agent/1.0

Content-type: application/json

Accept: application/json

Content-Length: 526   
   {
   "timestamp": 1571997083,
   "data": {
      "temperature": 20.613545532227,
      "humidity": 61.3828125,
      "battery": 3.47
   },
   "frame": "814962xxxx16a1dc5",
   "gatewayID": "AA555Axxxx0xx964",
   "othersGW": [],
   "clientID": "xxxx",
   "DevAddr": "011xxxxb",
   "DevEUI": "8cf9xxx000000xxx",
   "sensorInstallId": "8cf95xxx0000xxxx",
   "loraPort": 8,
   "fcnt": 1607,
   "rxpk": {
      "tmst": 2380181019,
      "time": "2019-10-25T09:51:23.472998Z",
      "chan": 0,
      "rfch": 0,
      "freq": 867.1,
      "stat": 1,
      "modu": "LORA",
      "datr": "SF7BW125",
      "codr": "4/5",
      "lsnr": 9,
      "rssi": -72,
      "size": 22,
      "data": "xxxxxxxxxxxxxxxx/YxWg=="
   }
}

so I would like to have data of temperature etc... and I don't know the node I have to use

Thanks a lot

2
The http-request node should return just JSON response as msg.payload when it returns. Or are you wanting to know how to extract just one field e.g. the temperature from that object? - hardillb
wanting to know how to extract just one field... yes the problem is the beginning of the message before {......} - chris35
Please edit the question to give more information about where that message is coming from. It looks like you are using curl rather than the built in tools to make a http request - hardillb
I use the input node" tcp " to listen the port and I receive the message / and in this message and I would like to extract temperature .... - chris35

2 Answers

1
votes

Having got a better view of what you are trying to do from the comments.

Using the TCP-in node is probably not the right approach for this. You will do much better using the HTTP-in/HTTP-out nodes as these will handle dealing with all the HTTP-header and sending a proper response to the client so the connection gets closed.

The HTTP-in node takes a path e.g. /input which will be appended to the Node-RED URL giving something like http://localhost:1880/input. It also takes a HTTP verb which in this case would be POST. You can find more details and lots of examples in the Node-RED cookbook here.

You will need to update the client to point to the correct path and port.

0
votes

You will need JSON node to convert the message string into an object.

Nodered has a very good documentation and hands on examples. See below for some help: