Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Jun 14, 2022 4:29:49 AM
May 3, 2022 4:47:21 AM
This article explains how to convert a JSON list to a CSV file or string using Qlik Application Automation.
The first two examples will use a "basic" JSON list. This means that every object in the list must contain all possible keys, even if the value for that key is null. In the 3rd part of this article, we'll cover lists that contain objects with varying keys.
This example will convert a JSON list to a CSV file on Amazon S3. Every object in the list must contain all possible keys, even if the value for that key is null.
This example will convert a non-nested JSON list to a CSV string.
You’ll find an exported example of this automation at the end of this article as convert_json_list_to_csv_string.json.
By nested lists, we mean lists that contain keys that have another list as a value. See the example below:
[
{
id: 1,
name: "Alice",
roles: ["admin", "write", "read"]
},
{
id: 2,
name: "Bob",
roles: ["read"]
}
]
Before we can use such a list to generate a CSV data structure (file or string), we'll need to flatten every object in the list before we apply the CSV formula or before we write it to a file.
Apply the flatten formula on every object individually instead of doing it for the full list. Applying the Flatten formula on the first object of the above example lists gives the following result:
{
"id": 1,
"name": "Alice",
"roles.0": "admin",
"roles.1": "write",
"roles.2": "read"
}
Note that the key names are now different, which will impact the CSV headers. Make sure you update the CSV formulas accordingly.
In the JSON structure below, the roles key has an empty list assigned as the value. This is what we call an empty nested list:
[
{
id: 1,
name: "Alice",
roles: []
}
]
If the Flatten formula is applied to the above item in the list, the following result is returned:
{
"id": 1,
"name": "Alice",
"roles": []
}
If the above object is used as input for the CSV formula, the object is tranformed to the following string:
id,name,roles
1,Alice,Array
While the following would be more accurate:
id,name,roles
1,Alice,
There are multiple approaches on achieving this: you could treat the object as a JSON string and remove all occurrences of [], or you could also use the ‘Remove empty properties’ formula from the formula picker. Apply this formula between the Flatten formula and the CSV formula. Note that this formula will remove both the key and value for the empty property. This influences the output of the CSV formula which means the headers must be specified or else the missing value is ignored.
All formulas together should now look like this:
The CSV formula can use different delimiters to separate strings. Use this functionality to handle special characters. For example, if you know your values contain commas, use a different delimiter than the comma symbol, for instance, a pipe symbol |.
See basic_exceptions_example.json at the beginning of this article for an example of these edge cases.
The information in this article is provided as-is and to be used at own discretion. Depending on tool(s) used, customization(s), and/or other factors ongoing support on the solution below may not be provided by Qlik Support.