web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Invalid type. Expected...
Power Automate
Unanswered

Invalid type. Expected Array but got Object

(0) ShareShare
ReportReport
Posted on by 3,506

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?

Categories:
I have the same question (0)
  • Pstork1 Profile Picture
    68,739 Most Valuable Professional on at

    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.

  • johnjohn123 Profile Picture
    3,506 on at

    @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

  • Pstork1 Profile Picture
    68,739 Most Valuable Professional on at

    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.

  • Barret Profile Picture
    652 Most Valuable Professional on at

    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.

  • piuskutty Profile Picture
    14 on at

    Try to check this below in Parse JSON
    "type": "array"
    with
    "type": ["array","object"]

     

  • Pstork1 Profile Picture
    68,739 Most Valuable Professional on at

    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.

  • piuskutty Profile Picture
    14 on at

    @Pstork1 Let me check and update both scenarios

  • kramaswamy-krak Profile Picture
    9 on at

    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. 

    kramaswamykrak_0-1679170404640.png

     

  • MarcinHanczaruk Profile Picture
    2 on at

    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']))

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 519 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 296 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard