I have the following JSON format, where sometimes i will have the Item containing single entry or an array .
here is how the JSON looks like when it has single Item:-
{
"?xml": {
"@version": "1.0",
"@encoding": "utf-8"
},
"RepeaterData": {
"Version": null,
"Items": {
"Item": {
"ArticleInput": {
"@type": "System.String",
"#text": "33333333"
},
"ArticleDescription": {
"@type": "System.String",
"#text": "desc33333"
},
"ArticleReturnable": {
"@type": "System.String",
"#text": "No"
},
"Qty": {
"@type": "System.Double",
"#text": "1"
}
}
}
}
}
here is how the JSON looks like when it has multiple Items:-
{
"?xml": {
"@version": "1.0",
"@encoding": "utf-8"
},
"RepeaterData": {
"Version": null,
"Items": {
"Item": [
{
"ArticleInput": {
"@type": "System.String",
"#text": "33333333"
},
"ArticleDescription": {
"@type": "System.String",
"#text": "desc33333"
},
"ArticleReturnable": {
"@type": "System.String",
"#text": "YES"
},
"Qty": {
"@type": "System.Double",
"#text": "1"
}
},
{
"ArticleInput": {
"@type": "System.String",
"#text": "33333333"
},
"ArticleDescription": {
"@type": "System.String",
"#text": "desc33333"
},
"ArticleReturnable": {
"@type": "System.String",
"#text": "YES"
},
"Qty": {
"@type": "System.Double",
"#text": "2"
}
}
]
}
}
}
so inside the Parse JSON i defined this schema:-
{
"type": "object",
"properties": {
"RepeaterData": {
"type": "object",
"properties": {
"Version": {},
"Items": {
"type": "object",
"properties": {
"Item": {
"type": "array",
"items": {
"type": "object",
"properties": {
"ArticleInput": {
"type": "object",
"properties": {
"#text": {
"type": "string"
},
"@type": {
"type": "string"
}
}
},
"ArticleDescription": {
"type": "object",
"properties": {
"#text": {
"type": "string"
},
"@type": {
"type": "string"
}
}
},
"ArticleReturnable": {
"type": "object",
"properties": {
"#text": {
"type": "string"
},
"@type": {
"type": "string"
}
}
},
"Qty": {
"type": "object",
"properties": {
"#text": {
"type": "string"
},
"@type": {
"type": "string"
}
}
}
},
"required": [
"ArticleInput",
"ArticleDescription",
"ArticleReturnable",
"Qty"
]
}
}
}
}
}
},
"?xml": {
"type": "object",
"properties": {
"@version": {
"type": "string"
},
"@encoding": {
"type": "string"
}
}
}
}
}
which will work if the Item contain multiple items, while it will fail if we have single item, and it will raise this error:-
[
{
"message": "Invalid type. Expected Array but got Object.",
"lineNumber": 0,
"linePosition": 0,
"path": "RepeaterData.Items.Item",
"schemaId": "#/properties/RepeaterData/properties/Items/properties/Item",
"errorType": "type",
"childErrors": []
}
]
So how i can fix this?
A bit late to the party but it may help others looking for the solution. Assuming that schema is changed to accept both array and object, you can use setProperty to update Items with itself but converted to array. If Items is already an array of Item it will stay as is, if there is just single Item object there, it will be converted to array of Item.
setProperty(body('Parse_JSON'), 'Items', array(body('Parse_JSON')?['Items']))
Hey @johnjohn123,
I had a similar situation - the input coming in either as an array or as an object. I solved it by using the "Configure Run After" option. Basically, I try to parse it as an array, and if it fails because it is in fact an object, I have a parallel branch which is configured to run after failure. In both cases, I append the result into an array object - in the case where it was originally an array, I use an Apply to Each block within which I put a Compose, and in the case where it isn't an array, I just use a normal Compose. Then, I combine the two parallel branches together. In my example I have just a Compose at the end, but you can do whatever you want with the result. Important is to configure the Run After for the combined result to run after both Success and Skipped of the two parallel branches.
As I mentioned, that will make the schema dynamic and the Parse JSON will work, but it will then push the error down the line to the first loop action where you try to use the array because it won't be an array it will be an object. As I said there is no easy way to fix this issue when converting from XML to JSON.
Try to check this below in Parse JSON
"type": "array"
with
"type": ["array","object"]
If you can't force the source of the JSON data to always send Items as an array even when it's a single result, about the only option I see is to add a step to check if Items is a single item or an array, then have parallel branches, each with a different schema. Not a great solution, but about your only option here.
Making the schema dynamic won't help, because formulas and actions expect either an object or an array of objects and can't be written to do both. This can be one of the problems when converting XML to JSON. There is no easy way to fix it.
@Pstork1 the schema is correct incase the JSON contain array of items, but will fail if it contain only one item.. so not sure how i can make the schema dynamic,, i tried to build the array my self as in this post @ https://powerusers.microsoft.com/t5/Building-Flows/%C3%9Csing-replace-inside-another-replace-will-raise-this-error/td-p/1530517 so not sure what i can do? thanks
Your JSON schema is incorrect. It specifies that Items is of Type object, when it can also be an array. You'll need to check the income JSON text for whether Items is an array or just a string and adjust it so that its always an array. You can't run a loop on an object, only an array.
stampcoin
87
Michael E. Gernaey
70
Super User 2025 Season 1
David_MA
48
Super User 2025 Season 1