jsonexport(
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.

##### Dealing with Null Values

The JSON format allows a data value to be unspecified, this is called a null value. To include null values in the generated JSON text, use the `error("null")` function, as shown in this code:

    local person
    person = initializedictionary(
        "ID",2192,
        "Name","Wilson, Thomas",
        "Title","Executive",
        "Location",error("null"),
        "Salary",error("null"))
    let jsonPerson = jsonexport(person)

The resulting JSON text will include redacted values for the location and salary:

    {
      "ID" : 2192,
      "Salary" : null,
      "Name" : "Wilson, Thomas",
      "Location" : null,
      "Title" : "Executive"
    }

The ?( function can be used to conditionally redact information. This example will redact the salary if it is over 80k:

    local person
    person = initializedictionary(
        "ID",EmployeeID,
        "Name",EmployeeName,
        "Title",EmployeeTitle,
        "Location",EmployeeOffice,
        "Salary",?(EmployeeSalary<80000,80000,error("null"))
    let jsonPerson = jsonexport(person)

Note: Use of the `error("null")` function with the jsonexport( function requires Panorama X 10.2 or later.

See Also

- databaseexportjson -- exports selected records in the current database in JSON format to a text file.
- exportjson -- exports the current database in JSON format.
- importjson -- imports a JSON array into the current database.
- importjsonline -- imports a JSON record into the current record.
- jsonexportline( -- exports the current database record in JSON format.
- jsonimport( -- converts JSON into a Panorama dictionary or data array.
- jsonscriptstring( -- converts text into a JSON string literal
- newdatabasewithjson -- creates a new database using a JSON file.
- tsvtojson( -- converts tab separated text into JSON formatted text.

History

Version | Status | Notes
10.2 | Updated | This function can now include null values in the JSON data.
10.0 | New | New in this version.