KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: What is a Cyclic Structure in JSON and why is it unable to be stringified?
PRODUCT: 4D | VERSION: 15.x | PLATFORM: Mac & Win
Published On: August 26, 2016

A Cyclic Structure is a structure that references itself. For example:

C_OBJECT($obj)
C_TEXT($json)
OB SET($obj;"o";1)
OB SET($obj;"this is cyclic";$obj)
The code above demonstrates a Cyclic Structure being built in 4D.

It is important to note that this type of structure cannot be stringified. If you attempt to stringify this data structure you can expect to see an error.

For exampple, the following code ran against the structure built above would produce an error:
$json:=JSON Stringify($obj)

The runtime error produced by the code above:
Error when executing the method "Method1" at line number 4<br /><br />JSON cannot stringify cyclic structures.<br /><br />Error code: 900<br />JSON cannot stringify cyclic structures.<br />xtoolbox<br />task -6, name: 'P_6'

The same error can be seen when attempting to view the Cyclic Structure in the 4D Debugger:

As you can see in the screenshot above, the Cyclic Structure can be expanded infinitely. Another term for this is recursion. Meaning that if it were stringified (written out as text), the text would go on forever and eventually fill the hard drive.