Type (Json function)

From m204wiki
Revision as of 16:05, 23 August 2016 by ELowell (talk | contribs) (→‎Syntax terms)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Get type of JSON object (Json class)

[Introduced in Model 204 7.6]

This function returns the type of a Json object.

Syntax

%jsonType = json:Type

Syntax terms

%jsonTypeJsonType enumeration value of the Json object type. If the Json object is null, the returned value is null.
Json Json object, which may be Null or any underlying Json object type (boolean, number, string, array, or object).

Usage notes

  • One would assume the type of a Json object would normally be established as part of the interface between the programs exchanging JSON data. As such, Type might not to be used very frequently. If the source of the JSON data is considered potentially unreliable then, rather than testing Type for each object, one might instead wrap all object references inside a Try/Catch for an InvalidJsonType exception.

Examples

The following example steps through an object and either prints the object string value of the object item (property) or the first item in the item (property) array, if it's an array. The determination of which to do is based on the result of the Type method:

b %json is object json %title is string len 64 %i is float %json = object %json("Lee") = "To Kill a Mockingbird" %json("Faulkner") = array("As I Lay Dying", "Absalom, Absalom", "The Sound and the Fury") %json("Hurston") = array("Jonah's Gourd Vine", "Their Eyes Were Watching God") %json("Ellison") = "Invisible Man" for %i from 1 to %json:count if %json:itemByNumber(%i):type eq array then %title = %json:itemByNumber(%i)(1):stringValue else %title = %json:itemByNumber(%i):stringValue end if printText {%json:nameByNumber(%i)}: {%title} end for end

This prints

Lee: To Kill a Mockingbird Faulkner: As I Lay Dying Hurston: Jonah's Gourd Vine Ellison: Invisible Man

See also