Skip to Content

Get XML element in JSON array using XML o JSON converter

Hi Experts,

I need to get an array of JSON format, but unable to achieve it using XML to JSON converter. Current output:

{"accounts":{"resourceId":"No reply","currency":"SEK","product":"Product","name":"Name","clearingNumber":"CLNum","bban":"Bban","_links":{"balances":"","transactions":""}}}

expected output:

{"accounts":[{"resourceId":"Test","currency":"000","product":"Medlemskonto","name":"000000","clearingNumber":"0000","bban":"00000","_links":{"balances":{"href":"v1/accounts/000000000/balances"},"transactions":{"href":"v1/accounts/000000/transactions"}}}]}
Can this be achieved using streaming option, on element node but I am getting the array as the table cannot be empty.
Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • May 15 at 01:50 AM

    Hi Vaishali

    If your XML payload has only 1 occurrence of accounts, then the standard XML-to-JSON converter will not automatically generate a JSON array for it. It only does so when the occurrence on the XML is more than 1.

    If you need to "force" the accounts field to be a JSON array even when there is only 1 element inside it, then you need to use a custom XML-to-JSON conversion. You can try out using XML2JSONConverter in FormatConversionBean for CPI. Parameter arrayFieldList as detailed in this blog post provides you the option to "force" the field to be generated as a JSON array.

    Regards

    Eng Swee

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Vaishali Rani

      I wasn't aware that the standard converter allows custom array conversion when using the Streaming feature (have never used that).

      I tried it out and it seems to work fine. Can you try this few things just to find out if your error could be due to other issues:-

      - Uncheck "Suppress JSON Root Element

      - Ensure your namespace setup is correct in both the converter, and also the overall IFlow (under Runtime Configuration)

      If it still does not work, please provide your XSD schema and the sample source payload.

      Regards

      Eng Swee

  • May 21 at 12:14 PM

    Hi ALl,

    I was able to solve the issue. Thanks.

    Add comment
    10|10000 characters needed characters exceeded