JSON in Ninox

To create a JSON variable (referred to as any or sometimes complex by the compiler), declare it as follows:

var myJSON := { 'prénom' : "Jacques", nom : "TUR" };

Accessing JSON data

You can use JSON values just like records by referencing var.key:

myJSON.'prénom';

Important notes:

  • The compiler does not verify the existence of the key.
  • The compiler is case-sensitive, so it’s essential to spell key names correctly.

JSON content

A JSON object can include text, numbers, arrays, and other JSON objects.
For example:

var myJSON := {
  'prénom' : "Mahatma",
  nom : "GANDHI",
  naissance : date(1869, 10, 2),
  age : 155,
  enfants : ["Harilal", "Manilal", "Ramdas"]
};

Converting JSON Values

Ninox treats all JSON values as any. To use them, you must convert them:

var myText := "";
myText := text(myJSON.'prénom');

var myDate := now();
myDate := date(myJSON.naissance);

var myNumber := 0;
myNumber := number(myJSON.age);

var myArray := [""];
myArray := for i in myJSON.enfants do text(i) end;

var 'myCoordinates' := {};
'myCoordinates' := myJSON.'coordonnées';

Adding or Removing Keys

To add or remove keys in a JSON object after its declaration, use setItem() and removeItem():

setItem(myJSON.'coordonnées', "adresse", "5, Tees January Marg, New Delhi, Inde");
removeItem(myJSON, "age");

Functions Returning JSON

Some Ninox functions return JSON, such as styled:

styled("texte", "red", "check")

Returns:

{"text":"texte","color":"#fff","background":"red","icon":"check"}

Iterating Over JSON

You can iterate over a JSON object with a for loop. See the For and While loops section for more details.