getJSON

getJSON

Overview

The getJSON function retrieves values from a JSON formatted text or a key-value collection, using a key.

Return Type

  • The return type will depend on the data type of the returned value.

Syntax

<variable>=<input>.getJson(<key>);

where,

ParameterData typeDescription
<variable>Any datatypeVariable that holds the returned value. Datatype of this parameter depends on the type of the returned value.
<input>TEXT
or
KEY-VALUE

The JSON formatted text or the key-value collection from which the specified key's value needs to be fetched.

Note:

  • If the input is not a JSON formatted TEXT or is not a KEY-VALUE collection, a runtime error will be thrown.
<key>Any datatype

The key whose value needs to be returned.

If the specified key is not found, null value will be returned.

Examples

The below example retrieves the ID of an employee from a JSON.

 employeeDetail="{\"response\" : {\"employee\": {\"id\" : \"EMP-001\", \"role\" : \"Developer\"}}}";   
 info employeeDetail.getJSON("response").getJSON("employee").getJSON("id");// returns EMP-001

The below example retrieves the IDs of all employees in a form of a Collection from a JSON.

 resp="{\"data\" : [{\"Id\":1, \"firstname\":\"John\", \"lastName\":\"Day\"},  {\"Id\":2, \"firstname\":\"Patricia\",\"lastname\":\"Boyle\"}]}";
 employeeList=resp.getJson("data");

 idList=Collection();
 for each employee in employeeList
 {
   idList.insert(employee.getJson("Id"));
 }
 info idList;//returns 1,2

Share this post : FacebookTwitter

Still can't find what you're looking for?

Write to us: support@zohodeluge.com