1. Home
  2. General information on JSON

General information on JSON

Introduction

JSON (JavaScript Object Notation) is a data exchange format for representing simple data structures and objects.

This is a standard. Implementations for generation and interpretation are therefore available in all programming languages. The description of the objects is based on a system of couple “cle” : “valeur”code>. In its syntax, a JSON uses the following characters:

  • { } > braces, to define an object
  • [ ] > square brackets, to define a list
  • : > two dots, to associate a key and its value
  • , > comma, to separate two elements of an object or a list

Example of JSON :

{
  "entrees": [
    { 
      "nom": "Soupe au potiron",
      "prix": 6
    },
    { 
      "nom": "Salade césar",
      "prix": 7
    },
    { 
      "nom": "Carpaccio de bœuf",
      "prix": 8
    }
  ],
  "plats": [
    { 
      "nom": "Steak de soja",
      "prix": 10
    },
    { 
      "nom": "Saucisse de Toulouse",
      "prix": 11
    },
    { 
      "nom": "Magret de canard",
      "prix": 12
    }
  ],
  "desserts": [
    { 
      "nom": "Mousse au chocolat",
      "prix": 6
    },
    { 
      "nom": "Tiramisu",
      "prix": 7
    },
    { 
      "nom": "Moelleux au chocolat + boule de glace",
      "prix": 8
    }
  ]
 }

On a restaurant menu, JSON could be translated as follows:

Entry :

  • Pumpkin soup (6€)
  • Caesar salad (7€)
  • Beef carpaccio (8€)

Dishes :

  • Soya steak (9€)
  • Toulouse sausage (10€)
  • Duck breast (11€)

Desserts :

  • Chocolate mousse (6€)
  • Tiramisu (7€)
  • Chocolate cake + scoop of ice cream (8€)

Here are the types available for element values in JSON :

  • String (between )
  • Number
  • Booleans (true / false)
  • Null value (null)
  • Table (between [ ])
  • Object (between { })

JSON does not specify a specific format for dates. It is however preferable to use the format given by the method toJSON() of the Date object in JavaScript (ISO8601 format).

Example: 2012-04-23T18:25:43.511Z

In a script, a JSON object can be built in two different ways:

1/ Directly “online”

var js_monJsonCarteResto = {
  entrees: [{
    "nom": "Soupe au potiron",
    "prix": 6
  },
  {
    "nom": "Salade cesar",
    "prix": 7
  },
  {
    "nom": "Carpaccio de boeuf",
    "prix": 8
  }],
  plats: [{
    "nom": "Steak de soja",
    "prix": 10
  },
  {
    "nom": "Saucisse de Toulouse",
    "prix": 11
  },
  {
    "nom": "Magret de canard",
    "prix": 12
  }],
  desserts: [{
    "nom": "Mousse au chocolat",
    "prix": 6
  },
  {
    "nom": "Tiramisu",
    "prix": 7
  },
  {
    "nom": "Moelleux au chocolat + boule de glace",
    "prix": 8
  }]
};

2/ Using methods liste.push(element) to insert an item into a list, and to insert an item into a list, and objet.cle(value) to associate a key and a value in an object.

var js_monJsonCarteResto = {};
var js_mesEntrees = [];
var js_mesPlats = [];
var js_mesDesserts = [];

js_mesEntrees.push({"nom": "Soupe au potiron","prix": 6});
js_mesEntrees.push({"nom": "Salade cesar","prix": 7});
js_mesEntrees.push({"nom": "Carpaccio de boeuf","prix": 8});

js_mesPlats.push({"nom": "Steak de soja","prix": 10});
js_mesPlats.push({"nom": "Saucisse de Toulouse","prix": 11});
js_mesPlats.push({"nom": "Magret de canard","prix": 12});

js_mesDesserts.push({"nom": "Mousse au chocolat","prix": 6});
js_mesDesserts.push({"nom": "Tiramisu","prix": 7});
js_mesDesserts.push({"nom": "Moelleux au chocolat + boule de glace","prix": 8});

js_monJsonCarteResto.mesEntrees = js_mesEntrees;
js_monJsonCarteResto.mesPlats = js_mesPlats;
js_monJsonCarteResto.mesDesserts = js_mesDesserts;

Using JSON in an Iterop script

Iterop variables manipulate strings and do not deal directly with JSON.

The JSON type does not exist in the Iterop variable types.
It is thus the string representation of this JSON which is stored in Iterop.

Also, to be able to manipulate these variables in JavaScript, it is necessary to use serialization / deserialization methods that JavaScript offers us:

  • At the beginning of the script, we parse a string to transform it into JSON.
  • At the end of the script, we stringify a JSON to save it as a string in an Iterop variable.

JSON.stringify()

This method is used to construct a string from a JSON object.

execution.setVariable("creerCarteResto_maCarteResto", JSON.stringify(js_monJsonCarteResto));

JSON.parse()

This method is used to construct a JSON Object from a string.

var js_maCarteResto = JSON.parse(creerCarteResto_maCarteResto);

// js_maCarteResto est un JSON, on peut donc le parcourir comme ceci :
var js_soupeAuPoriton = js_maCarteResto.entrees[0].nom

Creating a JSON from variables in an Iterop process

You will most likely need to integrate values from your Iterop variables.

Here is an example of how to integrate a new dish into the restaurant menu by retrieving values from Iterop variables:

var js_monJsonCarteResto = JSON.parse(creerCarteResto_maCarteResto);
var js_nouveauPlat = {};
js_nouveauPlat.nom = ajouterUnNouveauPlat_nomDuPlat;
js_nouveauPlat.prix = ajouterUnNouveauPlat_prixDuPlat;
js_monJsonCarteResto.mesPlats.push(js_nouveauPlat);
execution.setVariable("creerCarteResto_maCarteResto", JSON.stringify(js_monJsonCarteResto));
Updated on 13 November 2020

Was this article helpful?

Need Support?
Can't find the answer you're looking for?
Contact Support