Converting XML to json when xml request is multiple array

My XML request is:

<a1>
  <a>
    <a>2</a>
    <B>2</B>
  </a>
  <a>
    <S>2</S>
    <a>2</a>
    <B>2</B>
  </a>
</a1>

When I convert XML to JSON, I am getting this output:

{
  "a1": {
    "a": [
      {
        "a": 2,
        "B": 2
      },
      {
        "S": 2,
        "a": 2,
        "B": 2
      }
    ]
  }
}

But i want just

{
  "a1": [
    {
      "a": 2,
      "B": 2
    },
    {
      "S": 2,
      "a": 2,
      "B": 2
    }
  ]
}

Can anybody please help me with this ?

Solved Solved
0 2 383
1 ACCEPTED SOLUTION

This is an RTFM. Did you check out the "TreatAsArray" option in the XMLToJSON policy? It does exactly what you want.

Here's an example. I used this as an input payload.

<root>
  <a>
    <alpha>2</alpha>
    <beta>2</beta>
  </a>
  <a>
    <something>2</something>
    <alpha>2</alpha>
    <beta>2</beta>
  </a>
</root> 

As you can see I massaged the elements to have unique names, for clarity. But it's the same structure as your example.

When using this XMLToJSON configuration:

<XMLToJSON name='XMLToJSON-1'>
  <Source>request</Source>
  <OutputVariable>transformed_content</OutputVariable>
  <Options>
    <TreatAsArray>
      <!-- unwrap=true means omit the wrapper element -->
      <Path unwrap='true'>root/a</Path>
    </TreatAsArray>
  </Options>
</XMLToJSON>

I get this output:

{
  "root": [
    {
      "alpha": "2",
      "beta": "2"
    },
    {
      "something": "2",
      "alpha": "2",
      "beta": "2"
    }
  ]
}

View solution in original post

2 REPLIES 2

This is an RTFM. Did you check out the "TreatAsArray" option in the XMLToJSON policy? It does exactly what you want.

Here's an example. I used this as an input payload.

<root>
  <a>
    <alpha>2</alpha>
    <beta>2</beta>
  </a>
  <a>
    <something>2</something>
    <alpha>2</alpha>
    <beta>2</beta>
  </a>
</root> 

As you can see I massaged the elements to have unique names, for clarity. But it's the same structure as your example.

When using this XMLToJSON configuration:

<XMLToJSON name='XMLToJSON-1'>
  <Source>request</Source>
  <OutputVariable>transformed_content</OutputVariable>
  <Options>
    <TreatAsArray>
      <!-- unwrap=true means omit the wrapper element -->
      <Path unwrap='true'>root/a</Path>
    </TreatAsArray>
  </Options>
</XMLToJSON>

I get this output:

{
  "root": [
    {
      "alpha": "2",
      "beta": "2"
    },
    {
      "something": "2",
      "alpha": "2",
      "beta": "2"
    }
  ]
}