1 Reply Latest reply on Apr 8, 2020 7:41 AM by Prakash Jain

    Always return an array from util:toJSON?

    Andrzej Ruszczewski New Member

      Hi,

       

      I'm converting an xml like this:

       

      <root>

          <day>

              <date>2020-03-27</date>

              <working_day>true</working_day>

              <day_off>false</day_off>

          </day>

          <day>

              <date>2020-03-28</date>

              <working_day>false</working_day>

              <day_off>false</day_off>

          </day>

      <root>

       

      to a JSON like below using the util:toJSON function:

       

      {

          day: [

              {

                  "date": 2020-03-26,

                  "working_day": true,

                  "day_off": false

              },

              {

                  "date": 2020-03-27,

                  "working_day": false,

                  "day_off": false

              }

          ]

      }

       

       

      For the above example it works fine, however, when there is only one 'day' element in the XML:

       

      <root>

          <day>

              <date>2020-03-27</date>

              <working_day>true</working_day>

              <day_off>false</day_off>

          </day>

      </root>

       

      The 'day' in JSON changes into an object:

       

      {

          day: {

                  "date": 2020-03-26,

                  "working_day": true,

                  "day_off": false

              }

      }

       

      And I need it to be an array, even when it contains only a single element, like this:

       

      {

          day: [

                  {

                      "date": 2020-03-26,

                      "working_day": true,

                      "day_off": false

                  }

          ]

      }

       

       

      Is there a method to do this?