Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Incorrect JSON Path

cspra
Giga Expert

Hi,

How can I extract this "text":"nzur12345uap.XYZPROD.MSAD.XYZ.NET"?

Evaluator: com.glide.script.RhinoEcmaError: Cannot read property "0" from undefined
   script : Line(33) column(0)

==>  33:   gs.print(respJObj.data.result_sets[0].rows[0].data[2]);
 {
   "data":{
      "now":"2022/02/15 16:50:48 GMT-0000",
      "max_available_age":"",
      "result_sets":[
         {
            "age":0,
            "id":296900,
            "rows":[
               {
                  "id":1059534335,
                  "cid":3331268787,
                  "data":[
                     [
                        {
                           "text":"nzur12345uap.XYZPROD.MSAD.XYZ.NET"
                        }
                     ],
                     [
                        {
                           "text":"Windows Server 2016 Standard"
                        }
                     ],
                     [
                        {
                           "text":"1"
                        }
                     ]
                  ]
               }
            ]
         }
      ]
   }
}

Thanks

1 ACCEPTED SOLUTION

Dan H
Tera Guru

Hi, heres an example:

 

gs.log(respJObj.data.result_sets[0].rows[0].data[0][0].text);
 
If this has helped or solved your question please mark it as so!

View solution in original post

6 REPLIES 6

Hi,

 

I tried in a background script in my PDI:

 

Script:

 

 {
   "data":{
      "now":"2022/02/15 16:50:48 GMT-0000",
      "max_available_age":"",
      "result_sets":[
         {
            "age":0,
            "id":296900,
            "rows":[
               {
                  "id":1059534335,
                  "cid":3331268787,
                  "data":[
                     [
                        {
                           "text":"nzur12345uap.XYZPROD.MSAD.XYZ.NET"
                        }
                     ],
                     [
                        {
                           "text":"Windows Server 2016 Standard"
                        }
                     ],
                     [
                        {
                           "text":"1"
                        }
                     ]
                  ]
               }
            ]
         }
      ]
   }
}

 I assigned the output to the var "json"

and accessed it in the gs log.

 

Output:

*** Script: nzur12345uap.XYZPROD.MSAD.XYZ.NET

 

If you want to find out how to access specific lines in JSON take a look at jsonparser. Once you select a node, it will show the path to access it (highlighted yellow)

find_real_file.png




Hitoshi Ozawa
Giga Sage
Giga Sage

Tested with the following script.

var jsonStr = '{"data":{"now":"2022/02/15 16:50:48 GMT-0000","max_available_age":"","result_sets":[{"age":0,"id":296900,"rows":[{"id":1059534335,"cid":3331268787,"data":[[{"text":"nzur12345uap.XYZPROD.MSAD.XYZ.NET"}],[{"text":"Windows Server 2016 Standard"}],[{"text":"1"}]]}]}]}}';
var jsonObj = JSON.parse(jsonStr);
gs.log(jsonObj.data.result_sets[0].rows[0].data[0][0].text);

Execution result:

*** Script: nzur12345uap.XYZPROD.MSAD.XYZ.NET

Detailed Explanation in line:

{
   "data":{   <-- (1) get "data" object  -- "jsonObj.data"
      "now":"2022/02/15 16:50:48 GMT-0000",
      "max_available_age":"",
      "result_sets":[ <-- (2) get "result_sets" object. -- "jsonObj.data.result_sets" -->
                      <-- (3) "[" implies it's an array. Add "[0]" to get the first element of this array -- "jsonObj.data.result_sets[0] -->
         {
            "age":0,
            "id":296900,
            "rows":[  <-- (4) get "rows" object -- "jsonObj.data.result_sets[0].rows -->
                      <-- (5) "[" implies it's an array. Add "[0]" to get the first element of this array -- "jsonObj.data.result_sets[0].rows[0] -->
               {
                  "id":1059534335,
                  "cid":3331268787,
                  "data":[  <-- (6) get "data" object -- "jsonObj.data.result_sets[0].rows[0].data -->
                            <-- (7) "[" implies it's an array. Add "[0]" to get the first element of this array -- "jsonObj.data.result_sets[0].rows[0].data[0] -->
                     [      <-- (8) "[" implies it's an array. Add "[0]" to get the first element of this array -- "jsonObj.data.result_sets[0].rows[0].data[0][0] -->
                        {
                           "text":"nzur12345uap.XYZPROD.MSAD.XYZ.NET" <-- get "text" object -- jsonObj.data.result_sets[0].rows[0].data[0][0].text -->
                        }
                     ],
                     [
                        {
                           "text":"Windows Server 2016 Standard"
                        }
                     ],
                     [
                        {
                           "text":"1"
                        }
                     ]
                  ]
               }
            ]
         }
      ]
   }
}