JSON count a list from values results

Started by mattcarl, December 29, 2022, 06:57:20 AM

Previous topic - Next topic

mattcarl

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.




JTaylor

I believe ArrInfo will provide that info.

Jim

td

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
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mattcarl

That did it:

The missing link was the "jsConMap" step.

mData = jsConMap(aData)


Much obliged!