ItemByNumber (Json function)

From m204wiki
Revision as of 15:53, 18 February 2015 by Alex (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Return JSON object/array item value by item number (Json class)

[Introduced in Model 204 7.6]

This function returns a Json array or object item using the item number.

Syntax

%outJson = json:ItemByNumber( number) Throws InvalidJsonType

Syntax terms

%jsonJson object
Json Json object, which must be either an array or object. If a null or other underlying type is used, an InvalidJsonType exception is thrown.
number The item number of the object to return.

Usage notes

  • For array retrievals, there is no difference between ItemByNumber and [Item (Json property)|Item]] so it probably makes more sense to use Item if the Json object is known to be an array. The ItemByNumber function is allowed to operate on arrays so that the same code could be used on both arrays and objects.
  • The item number must be between 1 and the number of items (inclusive) in the array or object (Count). An invalid item number results in request cancellation.
  • For objects, the names of the items can be retrieved by number using the NameByNumber function.

Examples

The following example populates an object where each item is an array and then displays the name and value of each of the object items using NameByNumber and ItemByNumber:

b %json is object json %i is float %json = object %json('Packers') = array(1967, 1968, 1997, 2011) %json('Steelers') = array(1975, 1976, 1979, 1980, 2006, 2009) %json('Cowboys') = array(1972, 1978, 1993, 1994, 1996, 2009) %json('49ers') = array(1982, 1985, 1989, 1990, 1995) %json('Patriots') = array(2002, 2004, 2005, 2015) for %i from 1 to %json:count printText {%json:nameByNumber(%i)}: {%json:itemByNumber(%i)} end for end

This prints:

Packers: [1967,1968,1997,2011] Steelers: [1975,1976,1979,1980,2006,2009] Cowboys: [1972,1978,1993,1994,1996,2009] 49ers: [1982,1985,1989,1990,1995] Patriots: [2002,2004,2005,2015]

See also