NumberByName (Json function)

From m204wiki
Jump to navigation Jump to search

Get number of named item in JSON object (Json class)

[Introduced in Model 204 7.6]

This function returns the item number associated with a particular name in a Json object with an underlying object type.

Syntax

%number = json:NumberByName( unicode)

Syntax terms

%numberThe number of the item with the specified name. If no item has the name, a 0 is returned.
Json Json object which must have an underlying type of object. A null or any other underlying type throws an InvalidjsonType exception.
unicode The name to be found. As unicode suggests, this name is treated as unicode since all names are stored internally as unicode.

Usage notes

  • Probably the main use of NumberByName is to determine if a given name is present in an object. Since the Item property will return a null if either the requested name is not present in the object or if the value associated with that name is null, NumberByName is one way to distinguish those cases.

Examples

The following example populates an object with some values and then retrieves the values for names "Ostrich" and "Diplodocus". As you can see, the values are identical. However, the NumberByName method values for "Ostrich" and "Diplodocus" reveal that one is in the object but set to null while the other is simply not in the object.

b %json is object json %json = object %json("Tiger") = "Asia" %json("Lion") = "Africa" %json("Diplodocus") = null %json("Elk") = array("North America", "Asia") %json("Kangaroo") = "Australia" printText {~=%json("Ostrich")} printText {~=%json("Diplodocus")} printText {~=%json:numberByName("Ostrich")} printText {~=%json:numberByName("Diplodocus")} end

This prints:

%json("Ostrich")=null %json("Diplodocus")=null %json:numberByName("Ostrich")=0 %json:numberByName("Diplodocus")=3

See also