jsonexport(
DATA
)

The jsonexport( function converts a Panorama dictionary or data array into JSON.


Parameters

This function has one parameter:

data – The dictionary or data array to export as JSON.


Description

This function exports a Panorama dictionary or data array into JSON. (JSON is a data interchange format that is used by many web APIs. See [http://www.json.org][] for more information.)

The data passed to this function must be either a Panorama dictionary or a Panorama data array. Let’s take a look at an example of a dictionary.

local person
person = initializedictionary(
    "ID",2192,
    "Name","Wilson, Thomas",
    "Title","Executive",
    "Location","Santa Barbara",
    "Salary",83500)

We can display this information in JSON format using the jsonexport( function.

displaydata jsonexport(person)

The result will look like this:

{
  "ID" : 2192,
  "Salary" : 83500,
  "Name" : "Wilson, Thomas",
  "Location" : "Santa Barbara",
  "Title" : "Executive"
}

Note: As you can see in the example above, the order of the elements in the JSON record is not necessarily the same as the order the elements were defined in the Panorama dictionary. Panorama dictionaries have no defined order.

Data arrays can also be converted to JSON. Start with a data array, like this:

local colors
colors = dataarray("Red","Purple","Teal")

Note: If you have a text array, you must convert it to a data array using the importdataarray( function before converting it to JSON.

We can display this array in JSON format using the jsonexport( function.

displaydata jsonexport(colors)

The result will look like this:

[
  "Red",
  "Purple",
  "Teal"
]

In addition to basic dictionaries and arrays, data structures can be nested. Here is an example of an array nested within a dictionary.

local person
person = initializedictionary(
    "ID",3298,
    "Name","Zanhof, Steven",
    "Title","Sales Engineer",
    "Location","Tustin",
    "Territories",dataarray("Orange","Riverside","San Bernadino","San Diego"),
    "Salary",76400)

Here’s what this looks like in JSON format.

{
  "ID" : 3298,
  "Territories" : [
    "Orange",
    "Riverside",
    "San Bernadino",
    "San Diego"
  ],
  "Salary" : 76400,
  "Name" : "Zanhof, Steven",
  "Location" : "Tustin",
  "Title" : "Sales Engineer"
}

This example shows an array nested within a dictionary, but you can also start with an array and nest one or more dictionaries within the array. Dictionaries and data arrays can be nested as deep as you want.

Note: In addition to data interchange, the jsonexport( function is also very handy for displaying complicated data structures, which can be very handy in debugging code that handles these sorts of structures.


See Also


History

VersionStatusNotes
10.0NewNew in this version.