|
JSON Object- Encode |
Declaration
<AMJSON ACTIVITY="json_serialize" RESULTVARIABLE="text" TYPENAME="text" RESULTSTRUCTURE="text" MAXLENGTH="number"/>
Description
Converts structural data such as a data object or array to a JSON-formatted string.
Practical Usage
JSON is primarily used to transmit data between a server and web application, as an alternative to XML. This activity can be used to convert data objects or an array of objects to JSON format in order to effectively work with the data as text in a web page by using JavaScript. This lets you create a page where users interact with the data in the browser without requiring round trips to the server.
Parameters
General
Property | Type | Required | Default | Markup | Description |
---|---|---|---|---|---|
Object name | Text | Yes | (Empty) | TYPENAME="text" | The name of the object to convert to a JSON-formatted string. |
Populate variable with JSON string | Text | Yes | (Empty) | RESULTVARIABLE="varName" | The name of an existing variable to use to populate with the JSON-formatted string. For debugging purposes, when an object is encoded into a JSON string, the variable into which it is stored displays the current string value in the Variables Debug panel. |
Format JSON string | Yes/No | Yes | Yes | FORMAT="NO" | If selected (default), the JSON string is formatted with line breaks to make it easier to read and debug. If disabled, the JSON string output is a single line of code without line breaks to save space, which makes it more difficult to read and comprehend. |
Advanced
Property | Type | Required | Default | Markup | Description |
---|---|---|---|---|---|
Predefined type name (optional) | Text | No | (Empty) | RESULTSTRUCTURE="name" | Specifies the name of a predefined type. When any type names are predefined, clicking the box displays those names in a list. This parameter is optional. |
Maximum JSON string size in MB (Optional) | Number | No | (Empty) | MAXLENGTH="1" | Specifies the maximum JSON string size in megabytes (MB). This parameter is optional. |
Example
- The sample AML code below can be copied and pasted directly into the Steps Panel of the Task Builder.
- Parameters containing user credentials, files, file paths, and/or other information specific to the task must be customized before the sample code can run successfully.
Description
This sample task decodes JSON-formatted data into an object and re-encodes the object back to a JSON-formatted string.
<AMVARIABLE NAME="theString" VALUE="" />
<AMJSON ACTIVITY="json_deserialize" JSONSTRING="[
 {
 "id": "0001",
 "type": "donut",
 "name": "Cake",
 "ppu": 0.55,
 "batters":
 {
 "batter":
 [
 { "id": "1001", "type": "Regular" },
 { "id": "1002", "type": "Chocolate" },
 { "id": "1003", "type": "Blueberry" },
 { "id": "1004", "type": "Devils Food" }
 ]
 },
 "topping":
 [
 { "id": "5001", "type": "None" },
 { "id": "5002", "type": "Glazed" },
 { "id": "5005", "type": "Sugar" },
 { "id": "5007", "type": "Powdered Sugar" },
 { "id": "5006", "type": "Chocolate with Sprinkles" },
 { "id": "5003", "type": "Chocolate" },
 { "id": "5004", "type": "Maple" }
 ]
 },
 {
 "id": "0002",
 "type": "donut",
 "name": "Raised",
 "ppu": 0.55,
 "batters":
 {
 "batter":
 [
 { "id": "1001", "type": "Regular" }
 ]
 },
 "topping":
 [
 { "id": "5001", "type": "None" },
 { "id": "5002", "type": "Glazed" },
 { "id": "5005", "type": "Sugar" },
 { "id": "5003", "type": "Chocolate" },
 { "id": "5004", "type": "Maple" }
 ]
 },
 {
 "id": "0003",
 "type": "donut",
 "name": "Old Fashioned",
 "ppu": 0.55,
 "batters":
 {
 "batter":
 [
 { "id": "1001", "type": "Regular" },
 { "id": "1002", "type": "Chocolate" }
 ]
 },
 "topping":
 [
 { "id": "5001", "type": "None" },
 { "id": "5002", "type": "Glazed" },
 { "id": "5003", "type": "Chocolate" },
 { "id": "5004", "type": "Maple" }
 ]
 }
]" RESULTSTRUCTURE="decoded" />
<AMJSON ACTIVITY="json_serialize" RESULTVARIABLE="theString" RESULTSTRUCTURE="decoded" />
<AMSHOWDIALOG>%theString%</AMSHOWDIALOG>