4D v16.3

JSON Parse

Home

 
4D v16.3
JSON Parse

JSON Parse 


 

JSON Parse ( jsonString {; type} ) -> Function result 
Parameter Type   Description
jsonString  String in JSON string to parse
type  Longint in Type in which to convert the values
Function result  Boolean, Object, Pointer, Real, Text in Values extracted from JSON string

The JSON Parse command parses the contents of a JSON-formatted string and extracts values that you can store in a 4D field or variable. This command deserializes JSON data; it performs the opposite action of the JSON Stringify command.

In jsonString, pass the JSON-formatted string whose contents you want to parse. This string must be formatted correctly, otherwise a parsing error is generated. JSON Parse can therefore be used to validate JSON strings. 

Note: If you use pointers, you must call the JSON Stringify command before calling JSON Parse.

By default, if you omit the type parameter, 4D attempts to convert the value obtained into the type of the variable or field used to store the results (if one is defined). Otherwise, 4D attempts to infer its type. You can also force the type interpretation by passing the type parameter: pass one of the following constants, available in the Field and Variable Types theme:

Constant Type Value
Is Boolean Longint 6
Is date Longint 4
Is longint Longint 9
Is object Longint 38
Is real Longint 1
Is text Longint 2

Notes:

  • Real type values must be included in the range ±10.421e±10
  • In text type values, all special characters must be escaped, including quotes (see examples)
  • JSON dates must be in the format "\"YYYY-MM-DDTHH:mm:ssZ\"". The command considers that the 4D date contains a local time and not GMT.

Examples of simple conversions:

 C_REAL($r)
 $r:=JSON Parse("42.17") //$r = 42,17 (Real)
 
 C_LONGINT($el)
 $el:=JSON Parse("120.13";Is longint//$el=120
 
 C_TEXT($t)
 $t:=JSON Parse("\"Year 42\"";Is text// $t="Year 42" (text)
 
 C_OBJECT($o)
 $o:=JSON Parse("{\"name\":\"john\"}")
  // $o = {"name":"john"} (4D object)
 
 C_BOOLEAN($b)
 $b:=JSON Parse("{\"manager\":true}";Is Boolean// $b=true

Example of converting date type data:

 $test:=JSON Parse("\"1990-12-25T12:00:00Z\"")
  // $test=1990-12-25T12:00:00Z
 C_DATE($date)
 $date:=JSON Parse("\"2008-01-01T12:00:00Z\"";Is date)
  //$date=01/01/08

This example shows the combined use of the JSON Stringify and JSON Parse commands:

 C_TEXT($JSONContact)
 C_OBJECT($Contact;$Contact2)
 $Contact:=New object("name";"Monroe";"firstname";"Alan")
 
  // JSON Stringify: conversion of an object into a JSON string
 $JSONContact:=JSON Stringify($Contact)
 
  // JSON Parse: conversion of JSON string into a new object
 $Contact2:=JSON Parse($JSONContact)



See also 

Field and Variable Types
JSON PARSE ARRAY
JSON Stringify

 
PROPERTIES 

Product: 4D
Theme: JSON
Number: 1218

This command can be run in preemptive processes

 
HISTORY 

Created: 4D v14

 
TAGS 

JSON validator

 
ARTICLE USAGE

4D Language Reference ( 4D v16)
4D Language Reference ( 4D v16.1)
4D Language Reference ( 4D v16.2)
4D Language Reference ( 4D v16.3)