This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v20.6
JSON Stringify
|
JSON Stringify ( value {; *} ) -> Function result | ||||||||
Parameter | Type | Description | ||||||
value | Object, Mixed |
![]() |
Data to convert into JSON string | |||||
* | Operator |
![]() |
Pretty printing | |||||
Function result | Text |
![]() |
String containing serialized JSON text | |||||
The JSON Stringify command converts the value parameter into a JSON string. This command performs the opposite action of the JSON Parse command.
Pass the data to be serialized in value. It can be expressed in scalar form (string, number, date or time) or by means of a 4D object or collection.
Note: 4D dates will be converted either in "yyyy-mm-dd" or "YYYY-MM-DDThh:mm:sssZ" format according to the current database date setting (see the "Use date type instead of ISO date format in objects" option in the Compatibility page).
In the case of an object or a collection, you can include all types of values (see the JSON data types paragraph), with respect to the following JSON rules:
You can pass the optional * parameter to include formatting characters in the resulting string. This improves the presentation of JSON data (known as pretty formatting).
Conversion of scalar values:
$vc:=JSON Stringify("Eureka!") // "Eureka!"
$vel:=JSON Stringify(120) // "120"
$vh:=JSON Stringify(?20:00:00?) // "72000" seconds since midnight
SET DATABASE PARAMETER(Times inside objects;Times in milliseconds)
$vhms:=JSON Stringify(?20:00:00?) // "72000000" milliseconds since midnight
$vd:=JSON Stringify(!28/08/2013!) // "2013-08-27T22:00:00.000Z" (Paris timezone)
SET DATABASE PARAMETER(Dates inside objects;String type without time zone)
$vdd:=JSON Stringify(!28/08/2013!) // "2013-08-28T00:00:00.000Z"
Conversion of a string containing special characters:
$s:=JSON Stringify("{\"name\":\"john\"}")
// $s="{\\"name\\":\\"john\\"}"
$p:=JSON Parse($s)
// $p={"name":"john"}
Examples of serializing a 4D object with and without the * parameter:
C_TEXT($MyContact)
C_TEXT($MyPContact)
C_OBJECT($Contact;$Children)
OB SET($Contact;"lastname";"Monroe";"firstname";"Alan")
OB SET($Children;"firstname";"Jim";"age";"12")
OB SET($Contact;"children";$Children)
$MyContact:=JSON Stringify($Contact)
$MyPContact:=JSON Stringify($Contact;*)
//$MyContact= {"lastname":"Monroe","firstname":"Alan","children":{"firstname":"John","age":"12"}}
//$MyPContact= {\n\t"lastname": "Monroe",\n\t"firstname": "Alan",\n\t"children": {\n\t\t"firstname": "John",\n\t\t"age": "12"\n\t}\n}
The advantage of this formatting is clear when the JSON is shown in a Web area:
Example using a pointer to a variable:
C_OBJECT($MyTestVar)
C_TEXT($name ;$jsonstring )
OB SET($MyTestVar;"name";->$name) // object definition
// $MyTestVar= {"name":"->$name"}
$jsonstring :=JSON Stringify($MyTestVar)
// $jsonstring ="{"name":""}"
//...
$name:="Smith"
$jsonstring :=JSON Stringify($MyTestVar)
//$jsonstring = "{"name" : "Smith"}"
Serialization of a 4D object:
C_TEXT($varjsonTextserialized)
C_OBJECT($Contact)
OB SET($Contact;"firstname";"Alan")
OB SET($Contact;"lastname";"Monroe")
OB SET($Contact;"age";40)
OB SET($Contact;"phone";"[555-0100,555-0120]")
$varjsonTextserialized:=JSON Stringify($Contact)
// $varjsonTextserialized = "{"lastname":"Monroe","phone":"[555-0100,
// 555-0120]","age":40,"firstname":"Alan"}"
Serialization of a 4D object containing a date value (Paris time zone). The resulting string depends on the current database date settings.
C_TEXT($varjsonTextserialized)
C_OBJECT($Contact)
OB SET($Contact;"name";"Smith";"birthday";!22/10/1975!)
$varjsonTextserialized:=JSON Stringify($Contact)
"name":"Smith",
"birthday":"1975-10-21T22:00:00.000Z"
"name":"Smith",
"birthday":"1975-10-22"
Note: For more information on this setting, please refer to the Compatibility page.
Conversion of a collection (Paris time zone). The resulting string depends on the current database date settings.
C_COLLECTION($myCol)
C_TEXT($myTxtCol)
$myCol:=New collection(33;"mike";!28/08/2017!;False)
$myTxtCol:=JSON Stringify($myCol)
$myTxtCol="[33,"mike","2017-08-27T22:00:00.000Z",false]"
$myTxtCol="[33,"mike","2017-08-28",false]"
Note: For more information on this option, please refer to the Compatibility page.
Product: 4D
Theme: JSON
Number:
1217
Created: 4D v14
Modified: 4D v16 R4
Modified: 4D v16 R6
4D Language Reference ( 4D v20)
4D Language Reference ( 4D v20.1)
4D Language Reference ( 4D v20.2)
4D Language Reference ( 4D v20.3)
4D Language Reference ( 4D v20.4)
4D Language Reference ( 4D v20.5)
4D Language Reference ( 4D v20.6)