KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Arrays contained in JSON string will be parsed and typed as collections in v17
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: May 15, 2019

When parsing JSON strings and accessing arrays within the parsed object, javascript arrays are now typed as collections in contrast to v16 where they were typed as object arrays. Regardless of whether the javascript array only contains one or multiple data types, they are always typed as collections as shown below.

C_TEXT($jsonStr1;$jsonStr2;$jsonStr3)
$jsonStr1:="{\"someArr\":[1,2,3]}"
$jsonStr2:="{\"someArr\":[\"a\",\"b\",\"c\"]}"
$jsonStr3:="{\"someArr\":[1,\"b\",3]}"

C_OBJECT($someObj1;$someObj2;$someObj3)
$someObj1:=JSON Parse($jsonStr1)
$someObj2:=JSON Parse($jsonStr2)
$someObj3:=JSON Parse($jsonStr3)




When trying to assign or access these collections from the parsed object, simply create a new collection and assign it to the object attribute accordingly. Do not try to type the parsed array as a standard 4D array (text, longint, object, etc.) as it would result in a typing error.

C_COLLECTION($newCol)
$newCol:=$someObj3.someArr