Working with JSON data

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

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

Accessing JSON data

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

Ninox
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:

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

Converting JSON Values

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

Ninox
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():

Ninox
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:

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

Returns:

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

Iterating Over JSON

You can iterate over a JSON object with a for loop. See the Repeating an action: for and while section for more details.