How To Extract JSon Values From NodeRed JSon Object

How To Extract JSon Values From NodeRed JSon Object

How To Extract JSon Values From NodeRed JSon Object

In  this Post I will Step Through  on How to Extract JSON Value from Node Red JSon Object.

 

We will Use the below JSon object as an Example  ,

In this JSon Object we will Extract the value “truck ”  and the “political” value .

 

Value To Extract
Value To Extract

 

Before we deep dive into Setting up the node , Let go through on how to Read the JSON Object

“volume” is the second key Value of the Json Object ,and “volume” is 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” reside inside  the the First index of the  Array , and the Array reside  in index 0 of the “volume” Array.

To get the “political” Value . We understand  “political is the third key of the JSon Object

Nodes that you will Need

  1. Inject Node
  2. JSon convert Node
  3. function Node
  4. 2 Debug Node to output “truck ” and “political” Value

 

Build the Sample Node

Step 1

Drag out an inject Node and Configure as below, Click the  ”  … ” and Paste the Sample JSON into the text Area

{
    “direct”: “wagon”,
    “volume”: [
        [
            -1855311450.997438,
            “truck”,
            1689311981.2279916,
            “push”,
            708613102,
            “vessels”
        ],
        “carried”,
        “continent”,
        true,
        true,
        -1787726152.510077
    ],
    “political”: 1924784828.1228194,
    “tribe”: 606028185,
    “bear”: 245576558.18794966,
    “share”: “mighty”
}

 

Step 2

Drag out the JSon Objects Node and Configure as below

 

Step 3

Drag Out the Function Node  Click the Setup Tab and Configure as Below . When Done Click the  On Message Tab and  paste the Code below into the Text Area.

var container =[];
var truckValue = msg.payload.volume[0][1];
var politicalValue = msg.payload.political
// Create a new message with both extracted value
container = [{payload:truckValue},{payload:politicalValue}];
// Return the modified message
return container;

Step 4

Drag Out 2 Debug node  and Provide a logical name  . In my case i name it  ” Extract Truck Word ” and  ” Political Value ”

Step 5

Joint all the Nodes  and Click Deploy . Observe the Results

 

Results

 

Check out  Multiple Output From Node Red

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 6 =