4D v14.3

JSON Parse

Home

 
4D v14.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.

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 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\":\"jean\"}")
  // $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($MyContact)
 C_OBJECT($Contact)
 
  // JSON Stringify: conversion of JSON object into a JSON string
 $MyContact:=JSON Stringify("{\"name\":\"Monroe\",\"firstname\":\"Alan\"}")
  // $MyContact = "{\\"name\\":\\"Monroe\\",\\"firstname\\":\\"Alan\\"}"
  // JSON Parse: conversion of JSON string into a JSON object
 $Contact:=JSON Parse("{\"name\":\"Monroe\",\"firstname\":\"Alan\"}")
  // $Contact = {"name":"Monroe","firstname":"Alan"}

 
PROPERTIES 

Product: 4D
Theme: JSON
Number: 1218

 
HISTORY 

New
Created: 4D v14

 
SEE ALSO 

Field and Variable Types
JSON PARSE ARRAY
JSON Stringify

 
ARTICLE USAGE

4D Language Reference ( 4D v14 R2)
4D Language Reference ( 4D v14 R3)
4D Language Reference ( 4D v14.3)
4D Language Reference ( 4D v14 R4)