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?