Skip to main content

Notifications

Community site session details

Community site session details

Session Id : joEYvjeeuPt8DsN4WG7+FI
Power Automate - Building Flows
Unanswered

Invalid type. Expected Array but got Object

Like (0) ShareShare
ReportReport
Posted on 4 Apr 2022 01:48:55 by 3,502

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?

  • MarcinHanczaruk Profile Picture
    2 on 04 Jul 2024 at 16:42:09
    Re: Invalid type. Expected Array but got Object

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

  • kramaswamy-krak Profile Picture
    9 on 18 Mar 2023 at 20:15:53
    Re: Invalid type. Expected Array but got Object

    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

     

  • piuskutty Profile Picture
    12 on 06 Mar 2023 at 13:34:52
    Re: Invalid type. Expected Array but got Object

    @Pstork1 Let me check and update both scenarios

  • Pstork1 Profile Picture
    66,162 Most Valuable Professional on 06 Mar 2023 at 13:20:56
    Re: Invalid type. Expected Array but got Object

    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
    12 on 06 Mar 2023 at 10:51:06
    Re: Invalid type. Expected Array but got Object

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

     

  • bblake8480 Profile Picture
    642 Most Valuable Professional on 04 Apr 2022 at 15:36:37
    Re: Invalid type. Expected Array but got 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.

  • Pstork1 Profile Picture
    66,162 Most Valuable Professional on 04 Apr 2022 at 14:33:50
    Re: Invalid type. Expected Array but got Object

    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.

  • johnjohn123 Profile Picture
    3,502 on 04 Apr 2022 at 13:40:58
    Re: Invalid type. Expected Array but got Object

    @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
    66,162 Most Valuable Professional on 04 Apr 2022 at 13:01:32
    Re: Invalid type. Expected Array but got Object

    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.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Building Flows

#1
stampcoin Profile Picture

stampcoin 87

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 70 Super User 2025 Season 1

#3
David_MA Profile Picture

David_MA 48 Super User 2025 Season 1

Overall leaderboard