viewpoint-particle

Author Topic: JSON count a list from values results  (Read 174 times)

mattcarl

  • Newbie
  • *
  • Posts: 7
JSON count a list from values results
« 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.




JTaylor

  • Pundit
  • *****
  • Posts: 1880
    • Data & Stuff Inc.
Re: JSON count a list from values results
« Reply #1 on: December 29, 2022, 08:25:03 am »
I believe ArrInfo will provide that info.

Jim

td

  • Tech Support
  • *****
  • Posts: 4230
    • WinBatch
Re: JSON count a list from values results
« Reply #2 on: December 29, 2022, 08:31:04 am »
A simple example.

Code: Winbatch
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

  • Tech Support
  • *****
  • Posts: 4230
    • WinBatch
Re: JSON count a list from values results
« Reply #3 on: December 29, 2022, 08:41:28 am »
Another approach:

Code: Winbatch
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

  • Newbie
  • *
  • Posts: 7
Re: JSON count a list from values results
« Reply #4 on: December 29, 2022, 08:49:23 am »
That did it:

The missing link was the "jsConMap" step.

mData = jsConMap(aData)


Much obliged!