Date:

Share:

How To Extract JSon Values From NodeRed JSon Object – CreatifWerks

Related Articles

In this post I will go over how to extract a JSON value from it red junction JSON Object.

We will use the JSON object below as an example,

In this JSON object we will extract the value “truck” and the value “politic”.

value to extract

Before we dive deep into defining the node, we’ll go over how to read the JSON object

“volume” is the second key value of a Json object, and “volume” is a package as an Array. The array length of the “volume” array is equal to 6. The first array of “volume” which is also an array has an array length of 6.

to get the “truck” value. We understand that “truck” is inside the first index of the array, and the array resides at index 0 of the “volume” array.

To get the “political” value. We understand “Politic is the third key of JSon object

nodes you will need

  1. node injection
  2. JSON convert Node
  3. Node function
  4. 2 bugs at the junction for the “truck” output and the “politic” value.

Build the sample node

Level 1

Drag out an injection node and configure as below, click “…” and paste the sample JSON in the text area

{

“direct”: “cart”,

“volume”: [

        [

            -1855311450.997438,

            “truck”,

            1689311981.2279916,

            “push”,

            708613102,

            “vessels”

        ],

“carried”,

“mainland”,

Right,

Right,

-1787726152.510077

],

“Political”: 1924784828.1228194,

“tribe”: 606028185,

“Bear”: 245576558.18794966,

“Sharing”: “Awesome”

}

level2

Drag out the JSON Objects node and set as below

Step 3

Drag out the function node click on the definition tab and define as below. When done click on message tab and paste the code below in the text area.

var container =[];

var truckValue = msg.payload.volume[0][1];

var policeValue = msg.payload.political

// Create a new message with the two extracted values

Container = [{payload:truckValue},{payload:politicalValue}];

// Return the changed message

return container;

Step 4

Drag out the Debug 2 node and provide a sensible name. In my case I call it “word truck rescue” and “political value”

Step 5

Connect all nodes and press freeze. Pay attention to the results

results

Check for multiple output from the red node

Source

Popular Articles