ParseJSON

Revision as of 21:59, 9 March 2017 by Lchrisman (talk | contribs) (Parsing without a schema)


New to Analytica 5.0

This function requires the Analytica Enterprise edition or higher (e.g., Analytica Optimizer, ADE or CubePlan).

ParseJSON( json, objectSchema..., flags )

Parses JavaScript Object Notation (JSON) text in «json» and returns the parsed data. Parsing can be done with or without using «objectSchema». The «objectSchema», if used, helps you map the structure of the data onto indexes of your model.

The «json» text will most commonly be obtained from a call to ReadTextFile or ReadFromUrl.

Parameters

  • «json»: Text containing the JSON-formatted text to parse.
  • «objectSchema»: Zero or more schemas, where each schema describes the JSON class structure and the Analytica index that it maps onto. See Parsing with a schema. When no «objectSchema» is provided, «json» is parsed without a schema. See Parsing without a schema.
  • «flags»: (optional) A bit field of flags that control various aspects of parsing. Bit settings are
    • 1 = During schema-free parsing, create local indexes .Dim1, .Dim2, ... for arrays. Without this, each level of an array is returned as a reference to a list.

Parsing without a schema

When the «objectSchema» parameter is not specified,

ParseJSON( json )

parses the «json» data without using a schema.

Reading JSON objects

The top-level item in a JSON document should be a JSON-object. Here is an example of a JSON object.

Variable json1 
Definition: 
    '{ "title" : "1984",
       "author" : "George Orwell",
       "year" : 1949,
       "pages" : 336,
       "paperback" : true
     }'
ParseJSON(json1)
ParseJSON1.png

Notice that a local index named .Member is automatically created. The 'title' is text, the 'year' is a number.

Variable parse1a := ParseJSON(json1)
TypeOf(parse1a[.Member='title']) → 'Text'
TypeOf(parse1a[.Member='year']) → 'Number'

When there are nested objects, local indexes are created at each level. To prevent the indexes from combining into a rectangular array, the member object is placed within a reference.

Variable json2 
Definition: 
    '{ "title" : "1984",
       "author" : { "first" : "George", "last" : "Orwell" },
       "year" : 1949,
       "pages" : 336,
       "paperback" : true
     }'
Variable parse2 := ParseJSON(json2)
parse2ParseJSON2.png
#parse2[.Member='author']ParseJSON2b.png

Reading JSON arrays

Without using a schema, ParseJSON does not map array data to existing indexes that you might have. Without a schema, you have two options. In the first option, it will return arrays as lists and nested lists. In Analytica, to nest a list (and avoid having the implicit dimension combine with other indexes), the list is placed inside a reference. The first option is used unless the 1 bit of «flags» is set. The second option is to have ParseJSON created local indexes automatically, which are named .Dim1, .Dim2, etc., corresponding to the nesting level in «json». The second option produces a multi-dimensional array without nesting.

Variable json3 := '{ "data" : [ [ 1,2], [3,4], [5,6] ] }'
Variable parse3 := ParseJSON(json3)
#parse3[.Member='data']Parse3.png
#Slice(#parse3[.Member='data'],3)ParseJSON3b.png

Variable parse3b := ParseJSON(json3, flags:1)

#parse3[.Member='data']ParseJSON3a.png


See Also

Comments


You are not allowed to post comments.