Functions: ValueAtxxx

The ValueAtxxx functions are used with the WrapConnector interface to parse the information returned in JSON format for an HTTP request.

The WrapConnector real-time interface is used to make API calls from wraps to external systems. When a POST/PUT or GET HTTP request has been completed, the result is returned as a JSON structure in a text string that can be parsed using the ValueAtxxx functions described on this help page.

Introduction

The ValueAtxxx functions parse the JSON returned for an HTTP request. Using the provided key, they locate a key/value pair in the JSON and operate on the value. If the value is an array, the functions operate on the designated element of the array.

There are five ValueAtxxx functions:

  • ValueAt() – locates a value and returns it as a variant.
  • ValueAtAsString() – locates a value and returns it as a string.
  • ValueAtAsNumber() – locates a value and returns it as a number.
  • ValueAtAsBoolean() – locates a value and returns it as a Boolean.
  • ValueAtCheck() – locates a value and returns its datatype.

The syntax for the above functions is described in detail in the Function Reference below.

ValueAt returns the value as a variant

The basic version of ValueAt() parses the JSON returned by the HTTP request according to its parameters and returns the designated value as a variant.

=VALUEAT(json_as_string, key_and_index)

where json_as_string is the JSON structure or a cell reference for it, and key_and_index contains the nested keys for the value, and optionally an element index (1,2,..) if the value is an array.

The ValueAt() function returns a variant, either a string, number, or boolean depending on the value.

Examples
=ValueAt(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘wrench’:’big wrench’}}”,”httpstatus”) => 200

We have included the JSON in the function call above as a text string to make the example easier to read.  The key can refer to any key/value pair in the JSON. In this case, the key requests the value of httpstatus.

=ValueAt(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘wrench’:’big wrench’}}”,”payload”,”wrench”) => “big wrench”

The key can also refer to key/value pairs in the payload, which means you have to provide two keys. In the example above, we are requesting the value for wrench which is a part of payload.

=ValueAt(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘wrench’:[1,2]}}”,”payload”,”wrench”,2) => 2

An element index must be provided if the value is an array, and returns the corresponding array element. The first element of an array has the index 1. The example above requests the second element of the array wrench in the payload.

=ValueAt(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘rotation_lock’:TRUE}}”,”payload”,”rotation_lock”) => TRUE

Since ValueAt() returns a variant, it can also return a Boolean.

Typed functions

The ValueAt() function returns a variant, which may require additional validation of the result. We recommend that you instead use the typed versions of ValueAt that raise a “#N/A!” error if the result does not have the expected type. This automates the validation and makes it easy to catch unexpected results. The typed functions have the same syntax as the ValueAt() function.

ValueAtAsString

The ValueAtAsString() function returns a string. If the key references a value that is not a string, a “#N/A!” error is raised.

=ValueAtAsString(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘wrench’:’big wrench’}}”,”payload”,”wrench”) => “big wrench”

ValueAtAsNumber

The ValueAtAsNumber() function returns a floating point number. If the key references a value that is not numeric, a “#N/A!” error is raised.

=ValueAtAsNumber(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘wrench’:[1,2]}}”,”payload”,”wrench”,2) => 2

ValueAtAsBoolean

The ValueAtAsBoolean() function returns True or False. If the key references a value that is not Boolean, a “#N/A!” error is raised.

=ValuteAtAsBoolean(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘rotation_lock’:TRUE}}”,”payload”,”rotation_lock”) => TRUE

ValueAtCheck

You can use the ValueAtCheck() function to determine the datatype of a value in the JSON. ValueAtCheck is extra useful if the JSON does not return a static form, e.g. it includes an array with an unknown number of elements.

=ValueAtCheck(json_as_string, key_and_index) => -2,-1, 0, 1, 2, 3, 99

where key_and_index contains the nested keys for the value, and optionally an element index (1,2,..) if the value is an array.

The ValueAtCheck function returns the value type as an integer:

  • 1: the designated value is a string
  • 2: the designated value is a number incl Inf
  • 3: the designated value is a boolean

Additional return codes are documented in the Function Reference.

Examples

=ValueAtCheck(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘wrench’:’big wrench’}}”,”payload”,”wrench”) => 1
=ValueAtCheck(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘wrench’:[1,2]}}”,”payload”,”wrench”,2) => 2
=ValuteAtCheck(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘rotation_lock’:TRUE}}”,”payload”,”rotation_lock”) => 3

Function reference

Example

=ValueAt(“{‘httpstatus’:200,’msg’:’random text”, ‘payload’:{“‘wrench’:[1,2]}}”,”payload”,”wrench”,2) => 2

In the example above, a ValueAt function is used to parse the JSON data returned by an HTTP request.

The first parameter designates the JSON in a string variable. We have included the JSON in the function call as a text string to make the example easier to read. The second parameter designates the key and optional array index to return.

Format and parameters

=ValueAtxxx(json_as_string, key_and_index)

json_as_string

Provide the JSON returned by the HTTP request or reference a cell where the JSON is stored. In both cases the JSON needs to be a string.

key_and_index

Provide the nested keys for the value, and optionally an element index (1,2,..) if the value is an array. The first element of an array has the index 1.

Result

The ValueAt() function returns a variant.

The ValueAtAsString(), ValueAtAsNumber() and ValueAtAsBoolean() functions return a typed value according to their name. If the designated value is not of the expected type,  a “#N/A!” error is raised.

The ValueAtCheck() function returns a numeric return code:

  • -2: the JSON is invalid and cannot be parsed.
  • -1: the key/index does not exist. ValueAt() will return an empty string.
  • 0: the object at the path is a jobject or array, i.e. the path is too short to return terminal. ValueAt() will return the JSON for the key/index converted to string, mostly used for debugging.
  • 1: the designated value is a string.
  • 2: the designated value is a number incl Inf.
  • 3: the designated value is a boolean.
  • 99: the designated value is a terminal, but not a string, number, or boolean. It may be NaN or some other unexpected content.