How to Parse Object

Supriya25
Tera Guru

Hi All,

Kindly help me how to identify  "info" either it is starting with " { "  or  "[ { "

example 1:
{
data: {

      info: {
          "name": "servicenow"
             }

          }

  }

   

example 2:
{
data: {

      info: [
           {
          "name":"Servicenow"
            },{
          "name":"SNOW"
            }

          ]

      }

  }



1 ACCEPTED SOLUTION

Hi @Supriya25 

You can use:

 

var obj1 = {
    data: {
        info: {
            "name": "servicenow"
        }
    }
};

//Multiple Info objects
var obj2 = {
    data: {
        info: [{
            "name": "Servicenow"
        }, {
            "name": "SNOW"
        }]
    }
}

//Check if given input is an array
Array.isArray(YOUR_ARRAY);

//Function to check if a given input is JSON
function isJsonObject(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}

gs.info(Array.isArray(obj1.data.info)); //false
gs.info(Array.isArray(obj2.data.info)); //true
gs.info(isJsonObject(obj1.data.info)); //true
gs.info(isJsonObject(obj2.data.info)); //false

Thanks & Best regards,
Medi

View solution in original post

11 REPLIES 11

Thank you Medi..!

so total two methods

gs.info(obj1.data.hasOwnProperty("info2"))

 

gs.info(!!obj1.data.info2);

 

You are welcome @Supriya25!

As I believe I have answered all your queries, could you please mark the answers which helped you as helpful and accept the solutions so that it benefit future readers.

 


Thanks & Best regards,
Medi