All Things WinBatch > WinBatch

**************** New JSON Extender (Experimental) *******************

<< < (2/7) > >>

kdmoyers:
It works!  Thanks Tony!

td:

--- Quote from: stanl on September 10, 2021, 06:11:46 am ---
Well, didn't take long to get a failure... probably something I did. Used the same weather alerts URL I tested with Jim's extender and Newtonsoft Json.  Got an invalid argument on the jsValid() function.


--- End quote ---

It didn't take long to fix the problem either. You are passing the function a variant bstr which the WIL interpreter converts to the extender required Unicode as well as preserving the variant version in the variable. However, the extender is strict about parameter types so it expects only Unicode. We will adjust the parameter checking and post an update later today.

[edit] You can work around the problem by striping the bstr from the string doing something like "jData = StrTrim(jData)" before passing the string to any functions. Also, your script has a coding bug. The "Json" parameter to the jsSerialize function is not defined.

stanl:

--- Quote from: td on September 10, 2021, 06:49:31 am --- Also, your script has a coding bug. The "Json" parameter to the jsSerialize function is not defined.

--- End quote ---


Noted: it was a quickie test. Changed Json to Object and modified: jdata = StrTrim(request.ResponseText) so test now works.

stanl:
After the initial test worked, I modified this section to:



--- Code: Winbatch ---
If Valid
   Object = jsParse(jdata)
   TypesMap = jsObjMap(Object, @JsonValue)
   jsObjClose()
   MapFilePutCSV(cSerialize,TypesMap, ",",@true,0)
Else
   jsObjClose()
Endif
 

but not a lot of actionable data is returned. My objective is to process Json returns from REST API queries and convert to Excel. This would entail a Json object to map to headers[keys] and values with multiple values per header which is precluded by WB Maps, or I assume maps in general, where keys are distinct. For example:


{
parent: {
               key1:value
               key2:value }
}

parent: {
               key1:value
               key2:value }
}


}


would have parent as the Excel Tab Name, then


key1       key2
value      value
value      value




I'm sure the Extender can address this with some foreach looping.... but wanted to bring up for others who might want to make sense of Json data.

td:
The extender does not expand child objects in maps and you are correct that map keys must be unique. Objects are not expanded because maps cannot be nested. The purpose of returning maps is to provide quick a dirty access to simple JSON objects.  Child objects are represented as extender object handles in the WIL Map so you could create a sub-map for each child by access the handle in the parent map. You would have to write the script to deal with nesting depth up to 32 levels. The alternate solution would be to get a list of all the paths in the object and get the value of each.

We could provide an array or map as a user-selectable return value alternate for the jsKeyPaths function. This would improve script performance in cases where you are spinning through a long list of paths. 

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version