Dynamically Parse JSON as Object or Array
Source: Dev.to
Overview
This walkthrough explains how to dynamically parse JSON that may contain either an object or an array for the same element (the contacts field) when processing data in Azure Logic Apps.
Scenario
A client needed to ingest a list of clients from WorkflowMax into SQL. Each client record includes basic information and a list of contacts. WorkflowMax provides the data in XML, which is converted to JSON and stored in Azure Blob Storage.
The challenge: the contacts field is sometimes an array (when a client has multiple contacts) and sometimes a single object (when there is only one contact). This inconsistency makes it difficult to process the JSON in a Logic App.
Sample JSON
Multiple contacts (array)
{
"_id": "64fafdab0148d179b4bad3d6",
"index": 0,
"guid": "a5a30726-560b-4548-9219-d3e579ff4df0",
"name": "Joanna Stephenson",
"company": "SYNKGEN",
"contacts": [
{
"id": 0,
"name": "Church Cleveland",
"email": "churchclevelan@synkgen.com",
"age": 37
},
{
"id": 1,
"name": "Tabitha Gentry",
"email": "tabithagentry@synkgen.com",
"age": 26
}
]
}
Single contact (object)
{
"_id": "64fafdabea7f6eecf0e80b44",
"index": 1,
"guid": "3f273e26-2dec-4a56-a139-760977630590",
"name": "Lynnette Guerra",
"company": "COMTOURS",
"contacts": {
"id": 1,
"name": "Marcie Spence",
"email": "marciespence@comtours.com",
"age": 40
}
}
Problem
Azure Data Factory’s copy activity produces the contacts field as:
- an array when there are multiple contacts, or
- an object when there is only one contact.
Logic Apps cannot directly handle a schema that switches between an object and an array for the same property.
Reference Solution
A similar issue was discussed on the Power Users forum:
Dynamically Parse JSON as object or Array
The proposed solution involves checking the type or length of the contacts element at runtime and normalizing it to an array before further processing.
Implementation Steps in Logic Apps
-
Trigger – When a new JSON file is created in Blob Storage.
-
Parse JSON – Use the Parse JSON action with a schema that defines contacts as an array of objects.
-
Initialize Variable – Create an array variable, e.g.,
normalizedContacts. -
Condition – Add a Condition action to test whether
@length(body('Parse_JSON')?['contacts'])returns a number greater than 0.-
If true (contacts is already an array):
@setVariable('normalizedContacts', body('Parse_JSON')?['contacts']) -
If false (contacts is a single object):
@setVariable('normalizedContacts', createArray(body('Parse_JSON')?['contacts']))
-
-
For Each – Loop over
variables('normalizedContacts')to process each contact (e.g., upsert into SQL). -
Insert/Upsert Client – Use the appropriate connector to write the top‑level client details to the database.
Key Expression Details
-
Check if contacts is an array
@greater(length(body('Parse_JSON')?['contacts']), 0) -
Convert a single object to an array
createArray(body('Parse_JSON')?['contacts'])
Result
By normalizing the contacts field to an array regardless of the original structure, the Logic App can reliably iterate over each contact and perform the required database operations.