Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Coming Soon

...

hiddentrue

Function: toJson()

Converts an object, such as an array or JSON string, to JSON.

Syntax

toJson(object)

ArgumentTypeDescription
Object

Array

String

Object to be converted to JSON.


The returned JSON has the following syntax:

  • Objects are enclosed in curly brackets, or braces ( { } )

    • An object's key-value pairs are separated by a comma ( , )

    • The key and value in a pair are separated by a colon ( : )

    • Keys in an object are strings, whereas values may be a string, number, object, array, true, false, or null, including another object or an array

  • Arrays are enclosed in square brackets ( [ ] )

    • An array's values are separated by a comma ( , )

    • Each value in an array may be of a different type, including another array or an object

  • When objects and arrays contain other objects or arrays, the data has a tree-like structure

...

Examples

The data being converted:

CompanyIDCompanyNameIndustry
108Romaguera IncManufacturing
112Becker and SonsManufacturing
398Larson IncManufacturing

Example 1

Code Block
titleExample 1 Input
toJson(getCo)

...

Returns JSON containing an array of

...

objects. Each object contains 3 key-value pairs.

Code Block
titleExample 1 Output
{
  "getCo" : [ {
    "CompanyID" : 108,
    "CompanyName" : "Romaguera Inc",
    "Industry" : "Manufacturing"
  }, {
    "CompanyID" : 112,
    "CompanyName" : "Becker and Sons",
    "Industry" : "Manufacturing"
 }, {
    "CompanyID" : 398,
    "CompanyName" : "Larson Inc",
    "Industry" : "Manufacturing"
  } ]
}

Example 2

Code Block
titleExample 2 Input
{
forEach(
    	$company, toJson(getCo).getCo, 
		$company.CompanyName : $company
)
}
  • Returns JSON from a Lookup
  • Loops over the getCo array to generate a property pair of $company.CompanyName mapped to the company object
  • . Accessed original getCo array in that as a property. Looping over that and generating a property pair of $company.CompanyName mapped to the company object. The foreach returns a list of property pairs. Because their in the curly bracket they get composed into the JSON object 
code

Returns JSON containing a list of key-value pairs in curly brackets.

Code Block
titleExample 2 Output
{
  "Romaguera Inc" : {
    "CompanyID" : 108,
    "CompanyName" : "Romaguera Inc",
    "Industry" : "Manufacturing"
  },
  "Becker and Sons" : {
    "CompanyID" : 112,
    "CompanyName" : "Becker and Sons",
    "Industry" : "Manufacturing"
 },
  "Larson Inc" : {
    "CompanyID" : 398,
    "CompanyName" : "Larson Inc",
    "Industry" : "Manufacturing"
  }
}

Return JSON from Lookup

Creates a key/value pair of the CompanyName mapped to CompanyID

code

...

Example 3

Code Block
titleExample 3 Input
{
forEach(
    	$company, toJson(getCo).getCo,
		$company.CompanyName : $company.CompanyID
)
}

Returns 

code

Returns JSON containing a key-value pair of the CompanyName mapped to CompanyID

Code Block
titleExample 3 Output
{
  "Romaguera Inc" : 108,
  "Becker and Sons" : 112,
  "Larson Inc" : 398
}

Return JSON from Lookup

Creates a key/value pair of the CompanyName mapped to CompanyID

code

...

Example 4

Code Block
titleExample 4 Input
{
forEach(
    	$company, toJson(getCo).getCo, 
		$company.CompanyName : {$company,"CompanyName":toUpper($company.CompanyName)}
)
}

Returns 

code

Returns JSON containing the $company object, where the CompanyName field has been overwritten by an uppercase value.

Code Block
titleExample 4 Output
{
  "Romaguera Inc" : {
    "CompanyID" : 108,
    "CompanyName" : "ROMAGUERA INC",
    "Industry" : "Manufacturing"
  },
  "Becker and Sons" : {
    "CompanyID" : 112,
    "CompanyName" : "BECKER AND SONS",
    "Industry" : "Manufacturing"
  },
 "Larson Inc" : {
    "CompanyID" : 398,
    "CompanyName" : "LARSON INC",
    "Industry" : "Manufacturing"
  }
}