The CreatorCon Call for Content is officially open! Get started here.

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

If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

11 REPLIES 11

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

If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Thanks

one last Question , imagine "data" is very huge JSON out of that huge JSON  how to get to know "data" object has "info2" or not. 

 

var obj1 = {
    data: {
        info:  {
            "name": "servicenow"
        },
      info1:{
       "name":"QL"
        },
      "info2: {
        "name":"TU"
        }
         
    }
};

 

 

Hi @Supriya25 

You can do that as follow (notice there are !! not !):

var obj1 = {
    data: {
        info: {
            "name": "servicenow"
        },
        info1: {
            "name": "QL"
        },
        info2: {
            "name": "TU"
        }
    }
};

var obj2 = {
    data: {
        info: {
            "name": "servicenow"
        },
        info1: {
            "name": "QL"
        },
    }
};

gs.info(!!obj1.data.info2); // true
gs.info(!!obj2.data.info2); //false

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

gs.info(!!obj1.data.info2); // true
gs.info(!!obj2.data.info2); //false


Apart of this method any other way to to identify ?

@Supriya25 

There other ways. You can use this one also:

object.hasOwnProperty(KEY)

 

Example:

var obj1 = {
    data: {
        info: {
            "name": "servicenow"
        },
        info1: {
            "name": "QL"
        },
        info2: {
            "name": "TU"
        }
    }
};

var obj2 = {
    data: {
        info: {
            "name": "servicenow"
        },
        info1: {
            "name": "QL"
        },
    }
};

gs.info(obj1.data.hasOwnProperty("info2")); // true
gs.info(obj2.data.hasOwnProperty("info2")); //false

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.