-
2 Attachment(s)
cJsonPath class
OK, as I mentioned in the JsonConfig.pkg thread here, I found a need to use the same approach to getting either objects or values from deep inside pretty complex JSON.
I was finding that a real PitA to do, with my code getting very deeply nested. It is in a web app, so you tend to start fairly deep, but when you have lines [U]starting[/U] on column 65... well, that isn't good! Also, it is a whole lot of code I really didn't want cluttering up my logic, so...
I created my cJsonPath class.
It provides two public methods:
Function JsonAtPath, which returns the JSON object handle at the specified path.
Function ValueAtPath, which returns the value in the JSON object at the specified path.
If you use it right, I think it works OK; if you use it wrong, who knows? Life is too short to try to test all the myriad ways things can be done wrong! ;)
Usage might be:
[code]
Move (JsonAtPath(oJPath, hoJson, "foo.bar.baz[5][3].Mork[0][4][3]")) to hoObj
OR
Get ValueAtPath of oJPath hJoson "foo.bar.baz[5][3].Mork[0][4][3]" to sVal
[/code]
Here is the class package: [ATTACH]13475[/ATTACH]
I have tested it somewhat, but obviously you use it at your own risk. YMMV!
Please report bugs - most especially memory leaks (which were the very devil to eliminate!) - to mpeat at unicorninterglobal dot com.
Mike
-
Re: cJsonPath class
Looks like you are the path to JSON schema (all pun intended).
[URL]https://json-schema.org/[/URL]
-
Re: cJsonPath class
-
Re: cJsonPath class
-
Re: cJsonPath class
Thank you Klaus. :)
I have added the following handy (to me, anyway) function to it:
[code]
Function CountAtPath Handle hoJson String sPath Returns Integer
Integer iCount
Get JsonAtPath hoJson sPath to hoJson
If not hoJson ;
Function_Return 0
If (JsonType(hoJson) = jsonTypeArray) ;
Get MemberCount of hoJson to iCount
Send Destroy of hoJson
Function_Return iCount
End_Function
[/code]
Which lets you, obviously, find the number of array members at a given path: Move (CountAtPath(oJPath, hoJson, "foo.bar.baz")) to iCount.
One word of warning, while getting ValueAtPath to a boolean works OK, using it in a boolean test requires a little extra:
[code]
If (ValueAtPath(oJPath, hoJson, "foo.bar.baz[2]")) Showln "Value True" // Always says: "Value True"
Else Showln "Value False"
// So you need to write:
If ([COLOR=#ff0000][B]Integer[/B][/COLOR](ValueAtPath(oJPath, hoJson, "foo.bar.baz[2]"))) Showln "Value True"
Else Showln "Value False"
[/code]
The reason of course is that it is returning the [U]string[/U] "0" or "1" for JSON false or true, respectively. And [U]any[/U] non-empty string evaluates as true.
Mike
-
Re: cJsonPath class
Just wanted to add my thanks for this class, which I chose to use over the whole 'move all the JSON to a struct' thing because the JSON was massively over-engineered for just a few tid-bits of data.
-
Re: cJsonPath class
Thanks Dave... yes, it was getting rid of the the masses of JSON code that was the motivation, and I find structs clumsy and static when working with the flexibility of JSON.
Mike
-
Re: cJsonPath class
Thanks Mike,
This came in handy today.
For others.. the latest version can be located here:
[url]https://github.com/DataFlexCode/cJsonPath[/url]
--
Wil