cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AJ71
Helper II
Helper II

parse Json Failing due to Objects and Array in same XML call.

I am exporting an XML file of Customer Orders and am trying to parse the data to json but fail at the parse json point. Because some Orders  only have only 1 Item, The Output 'Item' comes in as object. But when an order has multiple Items the 'Item' output changes to array. 

 

  {
    "message": "Invalid type. Expected Array but got Object.",
    "lineNumber": 0,
    "linePosition": 0,
    "path": "orders.order[1].Product_Details.item",
    "schemaId": "#/properties/orders/properties/order/items/properties/Product_Details/properties/item",
    "errorType": "type",
    "childErrors": []
  },
  {
    "message": "Invalid type. Expected Array but got Object.",
    "lineNumber": 0,
    "linePosition": 0,
    "path": "orders.order[2].Product_Details.item",
    "schemaId": "#/properties/orders/properties/order/items/properties/Product_Details/properties/item",
    "errorType": "type",
    "childErrors": []
  },

 

 schema will only parse one or the other  so  if i set to Object i get error in the Reverse for the other orders i Expected Object but git array.

Can any one help with how i can allow objects at a certian point to be accepted or converted to array during the parse process?  unsure where to do this as before the pasre Json i cant get the body of the XML to json process i am using.  

flow.PNGflow fail.PNG

@DeepakS 

1 ACCEPTED SOLUTION

Accepted Solutions
mahoneypat
Super User
Super User

@AJ71  Did this work for you?  Please mark it as solution, if so.

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about the Power Platform, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

13 REPLIES 13
mahoneypat
Super User
Super User

Here is one hack approach that seems to work.  Please confirm.  Add a Compose with the expression below.  It converts the array or object to a string, checks for the presence of multiple objects by looking for ', {' with a carriage return in it.  If not found, it converts the single object into a single-element array. I used a Compose with your example input, so replace the outputs('Compose') in all 3 places with your content.

 

if(contains(string(outputs('Compose')), ',
  {'), outputs('Compose'), createArray(outputs('Compose')))
 
Pat
 




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about the Power Platform, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat  Thanks for that,  Running that on an order with only 1 item

[
  {
    "message": "Invalid type. Expected Object but got Array.",
    "lineNumber": 0,
    "linePosition": 0,
    "path": "",
    "schemaId": "#",
    "errorType": "type",
    "childErrors": []
  }
]

here are the first few lines of  schema

{
  "type": "object",
  "properties": {
    "orders": {
      "type": "object",
      "properties": {
        "order": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "Order_ID": {
                "type": "string"
              },

 

mahoneypat
Super User
Super User

Can you clarify if each flow run handles one (that may contain an object or an array)?  Or handling an array of things (that may contain an object or array)?  I think it's the latter, and would suggest a Select action in which you take the non-problematic ones as is, but use an if expression to convert any object-only ones to an array.  Your example schema should then specify an array.

If you can provide a full example input JSON array (that has some with object and some with array), a specific solution be suggested.

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about the Power Platform, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat  lol,  thanks , i am trying to learn and i go and i think i am reading you right?

 

My Imput is an array Of Orders that May contain and Object (1 Item) or  an array (many Items).

 

In the instance below the schema is set at "item" to be object,  here is the an input showing both Object and array. At the Item Point.  Orders  0, 1, 4,5 & 6 are objects (single item orders)  and Orders 2,3,7& 8 are array where multiple items have been ordered.

 

 

{
  "?xml": {
    "@version": "1.0",
    "@encoding": "UTF-8"
  },
  "orders": {
    "order": [
      {
        "Order_ID": "335",
        "Order_Status": "Awaiting Fulfillment",
        "Order_Date": "26/11/2021",
        "Customer_Email": "info@quickfixmechanics.com.au",
        "Customer_Phone": "07 4033 8860",
        "Total_Quantity": "3",
        "Total_Shipped": "0",
        "Date_Shipped": "",
        "Order_Notes": "",
        "Customer_Message": "Back order as requested when shirts were sent urgent",
        "Billing_First_Name": "Quick Fix Mechanics",
        "Billing_Last_Name": "1646",
        "Billing_Street_1": "395 Sheridan St",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "North Cairns  QLD  4870",
        "Product_Details": {
          "item": {
            "Product_Qty": "3",
            "Product_SKU": "R33011001082",
            "Product_Name": "Work Shorts",
            "Product_Variation_Details": "Size: 82R",
            "Product_Unit_Price": "23.00"
          }
        }
      },
      {
        "Order_ID": "336",
        "Order_Status": "Completed",
        "Order_Date": "26/11/2021",
        "Customer_Email": "info@boundarystauto.com.au",
        "Customer_Phone": "07 4724 3302",
        "Total_Quantity": "1",
        "Total_Shipped": "1",
        "Date_Shipped": "26/11/2021",
        "Order_Notes": "",
        "Customer_Message": "",
        "Billing_First_Name": "Boundary Street Automotive",
        "Billing_Last_Name": "1480",
        "Billing_Street_1": "127 Boundary Street",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "Railway Estate  QLD  4810",
        "Product_Details": {
          "item": {
            "Product_Qty": "1",
            "Product_SKU": "RLSB-SE-CU-10",
            "Product_Name": "Badge label - Service/Custom",
            "Product_Variation_Details": "Quantity: 1000, Service data line 1: Next service ..../...../....., Service data Line 2: Or at ....kms, Service data Line 3: Majhor Minor Tick Boxes, Service data line 4: ..................",
            "Product_Unit_Price": "280.00"
          }
        }
      },
      {
        "Order_ID": "337",
        "Order_Status": "Completed",
        "Order_Date": "26/11/2021",
        "Customer_Email": "john@westernportauto.com.au",
        "Customer_Phone": "03 5979 1127",
        "Total_Quantity": "11",
        "Total_Shipped": "11",
        "Date_Shipped": "26/11/2021",
        "Order_Notes": "",
        "Customer_Message": "",
        "Billing_First_Name": "Western Port Automotive Services",
        "Billing_Last_Name": "618",
        "Billing_Street_1": "3 / 166 Marine Parade",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "Hastings  VIC  3915",
        "Product_Details": {
          "item": [
            {
              "Product_Qty": "2",
              "Product_SKU": "RPOLO-3XL",
              "Product_Name": "Poly Cotton Polo Shirt",
              "Product_Variation_Details": "Size: 3XLarge",
              "Product_Unit_Price": "29.90"
            },
            {
              "Product_Qty": "3",
              "Product_SKU": "RPOLO-2XL",
              "Product_Name": "Poly Cotton Polo Shirt",
              "Product_Variation_Details": "Size: 2XLarge",
              "Product_Unit_Price": "29.90"
            },
            {
              "Product_Qty": "2",
              "Product_SKU": "RPOLO-XL",
              "Product_Name": "Poly Cotton Polo Shirt",
              "Product_Variation_Details": "Size: XLarge",
              "Product_Unit_Price": "29.90"
            },
            {
              "Product_Qty": "2",
              "Product_SKU": "RTJ-XL",
              "Product_Name": "Poly Cotton Technician Jumper",
              "Product_Variation_Details": "Size: XL",
              "Product_Unit_Price": "39.00"
            },
            {
              "Product_Qty": "2",
              "Product_SKU": "RTJ-2XL",
              "Product_Name": "Poly Cotton Technician Jumper",
              "Product_Variation_Details": "Size: 2XL",
              "Product_Unit_Price": "39.00"
            }
          ]
        }
      },
      {
        "Order_ID": "338",
        "Order_Status": "Completed",
        "Order_Date": "26/11/2021",
        "Customer_Email": "fleurieuauto@repcoservice.com",
        "Customer_Phone": "08 8552 9335",
        "Total_Quantity": "18",
        "Total_Shipped": "18",
        "Date_Shipped": "26/11/2021",
        "Order_Notes": "",
        "Customer_Message": "",
        "Billing_First_Name": "Fleurieu Automotive",
        "Billing_Last_Name": "1535",
        "Billing_Street_1": "D1 / 50-54 Maude Street",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "Victor Harbor  SA  5211",
        "Product_Details": {
          "item": [
            {
              "Product_Qty": "4",
              "Product_SKU": "RPOLOP-L",
              "Product_Name": "Polyester Polo Shirt",
              "Product_Variation_Details": "Size: Large",
              "Product_Unit_Price": "29.90"
            },
            {
              "Product_Qty": "8",
              "Product_SKU": "RPOLOP-XL",
              "Product_Name": "Polyester Polo Shirt",
              "Product_Variation_Details": "Size: XLarge",
              "Product_Unit_Price": "29.90"
            },
            {
              "Product_Qty": "6",
              "Product_SKU": "RPOLOP-2XL",
              "Product_Name": "Polyester Polo Shirt",
              "Product_Variation_Details": "Size: 2XLarge",
              "Product_Unit_Price": "29.90"
            }
          ]
        }
      },
      {
        "Order_ID": "339",
        "Order_Status": "Awaiting Fulfillment",
        "Order_Date": "26/11/2021",
        "Customer_Email": "info@autoplussa.com.au",
        "Customer_Phone": "08 8522 8080",
        "Total_Quantity": "1",
        "Total_Shipped": "0",
        "Date_Shipped": "",
        "Order_Notes": "",
        "Customer_Message": "",
        "Billing_First_Name": "Auto Plus SA",
        "Billing_Last_Name": "1532",
        "Billing_Street_1": "238 Curtis Road",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "Munno Para  SA  5115",
        "Product_Details": {
          "item": {
            "Product_Qty": "1",
            "Product_SKU": "RJ307M-S",
            "Product_Name": "Mens Geneva Jacket",
            "Product_Variation_Details": "Size: S, Embroidery: Business Logo  + Staff Member Name",
            "Product_Unit_Price": "75.55"
          }
        }
      },
      {
        "Order_ID": "340",
        "Order_Status": "Awaiting Fulfillment",
        "Order_Date": "26/11/2021",
        "Customer_Email": "office@kingsleyautocare.com",
        "Customer_Phone": "08 9409 2223",
        "Total_Quantity": "250",
        "Total_Shipped": "0",
        "Date_Shipped": "",
        "Order_Notes": "",
        "Customer_Message": "",
        "Billing_First_Name": "Kingsley Auto Care",
        "Billing_Last_Name": "962",
        "Billing_Street_1": "3 Moolanda Boulevard",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "Kingsley  WA  6026",
        "Product_Details": {
          "item": {
            "Product_Qty": "250",
            "Product_SKU": "RJB",
            "Product_Name": "Jelly Beans",
            "Product_Variation_Details": "Label: 50mm x 50mm",
            "Product_Unit_Price": "1.15"
          }
        }
      },
      {
        "Order_ID": "341",
        "Order_Status": "Awaiting Payment",
        "Order_Date": "26/11/2021",
        "Customer_Email": "athena_auto@aapt.net.au",
        "Customer_Phone": "07 3390 6988",
        "Total_Quantity": "1",
        "Total_Shipped": "0",
        "Date_Shipped": "",
        "Order_Notes": "",
        "Customer_Message": "",
        "Billing_First_Name": "Athena Automatics",
        "Billing_Last_Name": "232",
        "Billing_Street_1": "278 New Cleveland Road",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "Tingalpa  QLD  4173",
        "Product_Details": {
          "item": {
            "Product_Qty": "1",
            "Product_SKU": "RBCTW-25-PL-BO-NO",
            "Product_Name": "Biz Card - Workshop",
            "Product_Variation_Details": "Quantity: 250, Type: Plastic, Rear: Booking Rear, Card Holders: None, Affiliate Logos: MTAQ, RACQ, Rear Header: Business Name",
            "Product_Unit_Price": "275.00"
          }
        }
      },
      {
        "Order_ID": "342",
        "Order_Status": "Awaiting Payment",
        "Order_Date": "26/11/2021",
        "Customer_Email": "admin@maranoamechanical.com.au",
        "Customer_Phone": "07 4625 3663",
        "Total_Quantity": "8",
        "Total_Shipped": "0",
        "Date_Shipped": "",
        "Order_Notes": "",
        "Customer_Message": "",
        "Billing_First_Name": "Maranoa Mechanical",
        "Billing_Last_Name": "500",
        "Billing_Street_1": "12 Beardmore Place",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "St George  QLD  4487",
        "Product_Details": {
          "item": [
            {
              "Product_Qty": "3",
              "Product_SKU": "RMOTO-L",
              "Product_Name": "Poly Cotton Moto Shirt",
              "Product_Variation_Details": "Size: L, Embroidery: Staff Member Name+ Business Name, Details: Grant",
              "Product_Unit_Price": "40.00"
            },
            {
              "Product_Qty": "2",
              "Product_SKU": "RMOTO-L",
              "Product_Name": "Poly Cotton Moto Shirt",
              "Product_Variation_Details": "Size: L, Embroidery: Staff Member Name+ Business Name, Details: Jack",
              "Product_Unit_Price": "40.00"
            },
            {
              "Product_Qty": "1",
              "Product_SKU": "RMOTO-S",
              "Product_Name": "Poly Cotton Moto Shirt",
              "Product_Variation_Details": "Size: S, Embroidery: Staff Member Name+ Business Name, Details: Isaac",
              "Product_Unit_Price": "40.00"
            },
            {
              "Product_Qty": "2",
              "Product_SKU": "RMOTOP-M",
              "Product_Name": "Polyester Moto Shirt",
              "Product_Variation_Details": "Size: M, Embroidery: Staff Member Name+ Business Name, Details: Brandon",
              "Product_Unit_Price": "36.00"
            }
          ]
        }
      },
      {
        "Order_ID": "343",
        "Order_Status": "Awaiting Payment",
        "Order_Date": "26/11/2021",
        "Customer_Email": "admin@griffosautomotive.com.au",
        "Customer_Phone": "02 6552 3874",
        "Total_Quantity": "6",
        "Total_Shipped": "0",
        "Date_Shipped": "",
        "Order_Notes": "",
        "Customer_Message": "Can we please include these as part of the New RAS Member welcome pack?",
        "Billing_First_Name": "Griffo's Automotive",
        "Billing_Last_Name": "1692",
        "Billing_Street_1": "3 Beeton Parade",
        "Billing_Street_2": "",
        "Billing_Suburb__State__Zip": "Taree  NSW  2430",
        "Product_Details": {
          "item": [
            {
              "Product_Qty": "3",
              "Product_SKU": "RPOLO-2XL",
              "Product_Name": "Poly Cotton Polo Shirt",
              "Product_Variation_Details": "Size: 2XLarge, Extra Decoration: Business Logo Only",
              "Product_Unit_Price": "36.90"
            },
            {
              "Product_Qty": "1",
              "Product_SKU": "R2146-CHILI-10",
              "Product_Name": "Ladies Ezylin Blouse",
              "Product_Variation_Details": "Colour: Red, Style: Short Sleeve, Size: 10, Embroidery: Business Name only",
              "Product_Unit_Price": "74.75"
            },
            {
              "Product_Qty": "1",
              "Product_SKU": "R2145-CHILI-10",
              "Product_Name": "Ladies Ezylin Blouse",
              "Product_Variation_Details": "Colour: Red, Style: 3/4 Sleeve, Size: 10, Embroidery: Business Name only",
              "Product_Unit_Price": "74.75"
            },
            {
              "Product_Qty": "1",
              "Product_SKU": "R6V1CNBX10",
              "Product_Name": "Ladies Crew Neck Vest",
              "Product_Variation_Details": "Color: Black, Size: 10, Embroidery: Business Name only",
              "Product_Unit_Price": "45.00"
            }
          ]
        }
      }
    ]
  }
}

@Pstork1  This is the New discussion on the Next step after the disappearing outputs.

 

 

 whe 

mahoneypat
Super User
Super User

Thanks for providing the full JSON.  I think I got it working.

 

I put your JSON in a compose step; however, I had to remove the @ characters to avoid an invalid JSON error.  So you'll have to do the equivalent with a replace function.

mahoneypat_0-1638233750004.png

 

I then added a Select action to demonstrate.  I used this as the input to provide an Array to the Select

 

outputs('Compose')?['orders/order']
 
mahoneypat_1-1638233927289.png

 

 
and to get the other fields (for example)
 
item()?['Order_ID']
 
and for the item object/array, I used this expression (looked for }, in the string to detect an array)
 
if(contains(string(item()?['Product_Details/item']), '},'), item()?['Product_Details/item'], createArray(item()?['Product_Details/item']))
 
and got the desired output (an array of items regardless of input of object or array).
mahoneypat_2-1638233970195.pngmahoneypat_3-1638233986459.png

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about the Power Platform, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat  how are you getting the outputs? i cant get them?

how are you getting pastt the parse failing?

 

@mahoneypat  after about 10 hours of painstaking attempts i got to the Select stage.  Now how do i access the outputs from the select? 

mahoneypat
Super User
Super User

Glad you got it working.  We've all been there on taking a long time to figure out how to do something that should be simple, but hopefully you've learned a lot. How did you get rid of the @ ?  In any case, you can just use the output of the Select step (it is an array) downstream.  What is your desired final output/result?

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about the Power Platform, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Didnt qget complete flow working, only your 1 step to Select. still don't know how to retreive that data further on?

 

AJ71_0-1638746880105.png

 

I need to Populate forms , add data to sharepoint lists, send emails... 

mahoneypat
Super User
Super User

What issue are you seeing? You can use the Select output as in input to an Apply To Each action, and send email, create item, etc. inside it.  Or you could create an HTML table with the Select output and send the whole thing in an email. 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about the Power Platform, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


sorry , totally lost me there..  

I can pull most of the objects out  by using same item expression,  etc... But am getting stuck  at the array of the Items ,  i am populating a word Document  from template with a table referencing products, Previously i was able to Initiate and array then append that array  and use that out put but that is not working  and when i try to use your array the form recognises there is supposed to be data but some values are not there

 

 

AJ71_0-1638837021274.png

using your Expression for the items

 

if(contains(string(item()?['Product_Details/item']), '},'), item()?['Product_Details/item'], createArray(item()?['Product_Details/item']))

 

i am then running 

AJ71_1-1638837108559.png

AJ71_2-1638837154710.pngAJ71_3-1638837230260.png

The inputs from the Select are coming in 

AJ71_5-1638837534638.pngAJ71_6-1638837592466.png

and the inputs for the Products 

 

[
  {
    "Product_Qty": "2",
    "Product_SKU": "RVIT",
    "Product_Name": "Vehicle ID Tags",
    "Product_Variation_Details": "",
    "Product_Unit_Price": "25.00"
  },
  {
    "Product_Qty": "5",
    "Product_SKU": "RREUSEMASK",
    "Product_Name": "Reusable Face mask",
    "Product_Variation_Details": "",
    "Product_Unit_Price": "5.00"
  }
]

 

Outputs  from the populate 

 

 

But when i do the Form knows data is supposed to be there  but is not filling in the product details. you can see left screen all pdf file load ok to destination,  all individual outputs are loading OK  to the top of the document  but the products  fro the array expression Only the  ProductQty is coming in?

 

AJ71_4-1638837308170.png

 

 

 

mahoneypat
Super User
Super User

Since items is an array, you'll need to treat it as such inside your Apply to each.  For example, here is one way to get the Product Names out.  See expressions in the image.

 

mahoneypat_0-1638883161695.png

 

mahoneypat_1-1638883185407.png

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about the Power Platform, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


mahoneypat
Super User
Super User

@AJ71  Did this work for you?  Please mark it as solution, if so.

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about the Power Platform, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Solution Authors
Top Kudoed Authors
Users online (1,736)