Subject Re: xbJson - how to make the root element an array
From Adrian B <adrian@xb2.net>
Date Wed, 29 Jan 2020 15:00:04 -0500
Newsgroups xb2net

Chris,

You can use the xb2json function to convert your aTemp array to a JSON string. i.e.:

aTemp := {}

oJson := xbJson():new()
oJson:addVar('start_time','9:30')
oJson:addVar('end_time','10:30')
oJson:addVar('date', date())
AADD(aTemp,oJson)

oJson := xbJson():new()
oJson:addVar('start_time','11:30')
oJson:addVar('end_time','13:30')
oJson:addVar('date', date())
AADD(aTemp,oJson)

cResult := xb2json( aTemp )

// cResult

[
   {
      "start_time":"9:30",
      "end_time":"10:30",
      "date":"20200129"
   },
   {
      "start_time":"11:30",
      "end_time":"13:30",
      "date":"20200129"
   }
]


Chris Carmac Wrote:

> How do I make the root element of an xbJson object an array?  
>
> I'm try to product the following JSON data, according to the format specified by a third-party vendor I'm working with:
>
> [
>   {
>     "start_time": "09:30",
>     "end_time": "10:30",
>     "date": "2020-01-30"
>   },
>   {
>     "start_time": "11:30",
>     "end_time": "13:30",
>     "date": "2020-01-31"
>   }
> ]
>
> The root element of the JSON data is an array, with no tag name.  I tried this...
>
> aTemp := {}
>
> oJson := xbJson():new()
> oJson:addVar('start_time','9:30')
> oJson:addVar('end_time','10:30')
> oJson:addVar('date','2020-01-31')
> AADD(aTemp,oJson)
>
> oJson := xbJson():new()
> oJson:addVar('start_time','11:30')
> oJson:addVar('end_time','13:30')
> oJson:addVar('date','2020-01-31')
> AADD(aTemp,oJson)
>
> oJson2 := xbJson():new()
> oJson2:addvar('',aTemp)
> cResult := oJson2:asString()
>
> ... but it didn't work. I got this instead...
>
> {":[
>   {
>     "start_time": "09:30",
>     "end_time": "10:30",
>     "date": "2020-01-30"
>   },
>   {
>     "start_time": "11:30",
>     "end_time": "13:30",
>     "date": "2020-01-31"
>   }
> ]}
>
> Does anyone know the solution?  Any help will be greatly appreciated.
>
> Thanks!
>
> Chris


Recent messages in this thread
 
-# xbJson - how to make the root element an array Chris Carmac 29-Jan-2020 10:47 am
.-# Re: xbJson - how to make the root element an array Osvaldo Ramirez 29-Jan-2020 11:47 am
.|-# Re: xbJson - how to make the root element an array Osvaldo Ramirez 29-Jan-2020 11:54 am
.|.-# Re: xbJson - how to make the root element an array Chris Carmac 29-Jan-2020 01:24 pm
.|..\# Re: xbJson - how to make the root element an array Osvaldo Ramirez 29-Jan-2020 02:22 pm
.-# Re: xbJson - how to make the root element an array (Current message) Adrian B 29-Jan-2020 03:00 pm
..\# Re: xbJson - how to make the root element an array Chris Carmac 30-Jan-2020 03:47 pm