Results 1 to 8 of 8

Thread: cJsonPath class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,812

    Default 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 starting 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
    Here is the class package: cJsonPath.pkg

    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
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2009
    Location
    Peoria, IL
    Posts
    5,491

    Default Re: cJsonPath class

    Looks like you are the path to JSON schema (all pun intended).

    https://json-schema.org/
    Chuck Atkinson

    "No matter how confounding the case, Bob Worsley always finds the quaesitum."

  3. #3
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,812

    Default Re: cJsonPath class


  4. #4

    Default Re: cJsonPath class

    How very, very nice.

  5. #5
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,812

    Default 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
    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 (Integer(ValueAtPath(oJPath, hoJson, "foo.bar.baz[2]"))) Showln "Value True"
        Else Showln "Value False"
    The reason of course is that it is returning the string "0" or "1" for JSON false or true, respectively. And any non-empty string evaluates as true.

    Mike

  6. #6
    Join Date
    Feb 2009
    Location
    Cayman Islands
    Posts
    3,969

    Default 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.
    I should be on a beach ...

  7. #7
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,812

    Default 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

  8. #8

    Default Re: cJsonPath class

    Thanks Mike,

    This came in handy today.

    For others.. the latest version can be located here:
    https://github.com/DataFlexCode/cJsonPath

    --
    Wil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •