This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v20 R7
JSON Stringify

JSON Stringify 


 

JSON Stringify ( value {; *} ) -> Function result 
Parameter Type   Description
value  Object, Mixed in Data to convert into JSON string
Operator in Pretty printing
Function result  Text in 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:

  • String values must be enclosed in quotes. All Unicode character can be used except for special characters that must be preceded by a backslash.
  • Numbers: interval of ±10.421e±10
  • Booleans: "true" or "false" strings
  • Dates: Text type in "yyyy-mm-dd" or "\"YYYY-MM-DDTHH:mm:ssZ"\" format, according to the current database date setting (see above).
  • Times: Real type (number of seconds by default)
    Notes:

    • Picture attributes are converted to the following string: "[object Picture]".
    • Pointers to a field, variable or array are evaluated when stringified

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:

  • Standard formatting:
  • Pretty formatting:

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)

  • If the "Use date type instead of ISO date format in objects" option is not checked:
    "name":"Smith",
    "birthday":"1975-10-21T22:00:00.000Z"
  • If the "Use date type instead of ISO date format in objects" option is checked:
    "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)

  • If the "Use date type instead of ISO date format in objects" option is not checked:
    $myTxtCol="[33,"mike","2017-08-27T22:00:00.000Z",false]"
  • If the "Use date type instead of ISO date format in objects" option is checked:
    $myTxtCol="[33,"mike","2017-08-28",false]"

Note: For more information on this option, please refer to the Compatibility page.



See also 

JSON Parse
JSON Stringify array

 
PROPERTIES 

Product: 4D
Theme: JSON
Number: 1217

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v14
Modified: 4D v16 R4
Modified: 4D v16 R6

 
ARTICLE USAGE

4D Language Reference ( 4D v20 R7)