WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: mattcarl on December 29, 2022, 06:57:20 AM

Title: JSON count a list from values results
Post by: mattcarl on December 29, 2022, 06:57:20 AM
Hello,
I'm using the JSON extender and can't seem to find a way to count the items in a response value.


;;;;; the JSON line:
"Amenities": ["air conditioning", "hot tub", "kitchen", "parking", "pets allowed", "satellite or cable TV", "wireless internet"]

;;;;; getting the value
theValue      = jsValueGet(Json, nextFieldPath : "[Amenities]")
response>>   *arr15376*

;;;;; getting the Type
TypesMap = jsConMap(theValue, @JsonType)
response>>  <array>

I know I have captured the the data as values such as:
[Amenities][0] = "air conditioning"
[Amenities][1] = "hot tub"
[Amenities][2] = "kitchen"
etc....

The WIL array functions keep saying "theValue" is not an array.
Is there a way to convert JSON <array> to WIL array? or List? or String?

I'm looking for a way to count the number of items in this list?
Thanks in advance.



Title: Re: JSON count a list from values results
Post by: JTaylor on December 29, 2022, 08:25:03 AM
I believe ArrInfo will provide that info.

Jim
Title: Re: JSON count a list from values results
Post by: td on December 29, 2022, 08:31:04 AM
A simple example.

Code (winbatch) Select
AddExtender("ilcjs44i.dll", 0, "ilcjs64i.dll")

;; Turn fragment into valid JSON data for this example.
jData = `{"Amenities": ["air conditioning", "hot tub", "kitchen", "parking", "pets allowed", "satellite or cable TV", "wireless internet"]}`

hData =  jsParse(jData)
aData = jsValueGet(hData,"[Amenities]")

mData = jsConMap(aData)
nElem = ArrInfo(mData, 1)

Message("Number of Elements", nElem)
exit
Title: Re: JSON count a list from values results
Post by: td on December 29, 2022, 08:41:28 AM
Another approach:

Code (winbatch) Select
AddExtender("ilcjs44i.dll", 0, "ilcjs64i.dll")

;; Turn fragment into valid JSON data for this example.
jData = `{"Amenities": ["air conditioning", "hot tub", "kitchen", "parking", "pets allowed", "satellite or cable TV", "wireless internet"]}`

hData =  jsParse(jData)
aData = jsValueGet(hData,"[Amenities]")
aPaths = jsKeyPaths(aData, "")
nElem = ArrInfo(aPaths, 1)

Message("Number of Elements", nElem)
Title: Re: JSON count a list from values results
Post by: mattcarl on December 29, 2022, 08:49:23 AM
That did it:

The missing link was the "jsConMap" step.

mData = jsConMap(aData)


Much obliged!