cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AyCN
Regular Visitor

Force array format when converting XML to JSON

Hi,

 

I'm using the json funtion to convert from xml with this structurexml.JPG

 

As you can see, some elements have several children, some have no children and some have only one.

The problem when converting to json is that those who have several children get an array of 'Child' but those with just one child get an object, as seen below: 

 

json.JPG

 

This causes a problem when formatting the json in the Parse JSON node.

The schema I'm going for is this:

schema.JPG

 

But when trying to parse the json I get this error:

 

[
  {
    "message""Invalid type. Expected Array but got Object.",
    "lineNumber"0,
    "linePosition"0,
    "path""Root.Person[2].Child",
    "schemaId""#/properties/Root/properties/Person/items/properties/Child",
    "errorType""type",
    "childErrors": []
  }
]

 

If I change the schema to expect an object, the same thing happens but with reversed.

Is there a way to force the 'Child' nodes to convert always as an array whether there's one or many of them?

I hope I made myself clear.

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @AyCN 

 

I did tested this now.

 

image.png

 

If you add the additional types it should work. Its similar to @Pstork1  suggested.

 

Here is my test result

 

image.png



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Power Automate Video Tutorials

View solution in original post

3 REPLIES 3
Pstork1
Dual Super User
Dual Super User

I don't know of any way to impose a format on the conversion.  But you can fix the error in the parse JSON by changing the type to expect either an array or a string value.  But that won't really solve the problem since you'll now have to treat each Child value differently when you go to use them.  Here's the fix for the JSON schema

"Child": {
"type": [
      "array",
      "string"
],
"items": { }
}

 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Join me at 365 and PWR EduCon in Chicago
EduCon Chicago 2022

Hi @AyCN 

 

I did tested this now.

 

image.png

 

If you add the additional types it should work. Its similar to @Pstork1  suggested.

 

Here is my test result

 

image.png



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Power Automate Video Tutorials
Pstork1
Dual Super User
Dual Super User

@abm Adding the additional types is exactly what I recommended.  The only problem is that now the Child element may be a string or an array.  The rest of the flow will have to deal with that, which could be a problem.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Join me at 365 and PWR EduCon in Chicago
EduCon Chicago 2022

Helpful resources

Announcements
Register for a Free Workshop.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Users online (1,851)