问题
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
回答1:
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.
回答2:
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:
- This is great example how to to use the json node
- This describes how to work with JSON data
来源:https://stackoverflow.com/questions/58558447/extract-json-with-nodered