Skip to main content

Create a JSON Array and store it in a Variable

How to create a JSON Array and store and access it with a variable.

Ossi Galkin avatar
Written by Ossi Galkin
Updated over 2 years ago

Written for Frends version 5.6.

JSON Arrays can be made using JArrays available through Json.NET (https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm). In this guide, the following simple JSON Array will be created inside a Foreach Loop:

[{"count":0},{"count":1},{"count":2}]

Prerequisites

You will need the following prerequisites to follow through with this guide:

  • Editing rights for your Frends Tenant.

Creating JArrays 

JArrays can be made straight from the existing object or built Element by Element. In this guide JArrays are built element by element inside a Foreach Loop. 

An empty JArray can be made with the following expression:

new JArray()

When this is done inside a code block, the newly created JArray can be assigned to a variable, with the name ArrayOfCounts:

Next, a Foreach Loop that will add elements is created. Its default parameters are sufficient:

ArrayOfContents can be accessed in Frends anywhere by just writing 

#var.ArrayOfCounts 

New elements can be added to ArrayOfCounts with method .Add(). New elements being added can be JObjects, for example, that can be made from C# objects with method: JObject.FromObject. In this case, objects contain only the index of the Foreach Loop with the name "count", so together the whole expression that will append elements in ArrayOfCounts is: 

#var.ArrayOfCounts.Add(JObject.FromObject(new {count = #var.i_index}))

It is important not to assign Variable, because method .Add() will return nothing (void), and if it is assigned to a Variable it will produce a runtime error. 

The complete Process will look similar to the following Process:

When this Process is run, it will make the following JArray and it is stored in a variable named ArrayOfCounts:

[{"count":0},{"count":1},{"count":2}]


Next, learn how to Create a simple FRENDS API.

Did this answer your question?