4D v14.3JSON Parse |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v14.3
JSON Parse
|
JSON Parse ( jsonString {; type} ) -> Function result | ||||||||
Parameter | Type | Description | ||||||
jsonString | String |
![]() |
JSON string to parse | |||||
type | Longint |
![]() |
Type in which to convert the values | |||||
Function result | Boolean, Object, Pointer, Real, Text |
![]() |
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:
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:
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"}
Product: 4D
Theme: JSON
Number:
1218
Created: 4D v14
Field and Variable Types
JSON PARSE ARRAY
JSON Stringify
4D Language Reference ( 4D v14 R2)
4D Language Reference ( 4D v14 R3)
4D Language Reference ( 4D v14.3)
4D Language Reference ( 4D v14 R4)