B4A Library [B4X] B4X Library - JSON Path

This library creates a new way to access json values. You literally create a path and get the value you want just by studying the expected JSON (if you get it from a request). The object is JSONPath and by getting the {JSONPath}.Instructions you will get the following code where you can easily understand how it works. The path can be constructed with statements or be set directly. The benefit from this is the following: If you create a program that processes a JSON, then if this JSON changes, just by updating (f.e. through the internet) the JSONPath to the new path you can get the required values directly again without any change to the code. In order to see how the path is constructed in order to set it afterwards see the Log(jsp.JSONPath). It works in all three IDEs (B4A, B4i, B4J).


B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private jsp As JSONPath
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    jsp.Initialize
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub Button1_Click
    jsp.BuildPath.GetJSONObjectValueWithTheKey("mylists").GetJSONArrayValueInPosition(2).GetJSONArrayValueInPosition(0).GetJSONObjectValueWithTheKey("mykey")
    Log(jsp.JSONPath)

    Dim sJSON As String = $"{"mylists":[[{"mykey":"nikos1"},{"mykey":"george1"}],[{"mykey":"nikos2"},{"mykey":"george2"}],[{"mykey":"nikos3"},{"mykey":"george3"}]]}"$
    Log(jsp.GetValueAccordingToPath(sJSON))
End Sub
 

Attachments

  • NHJSONPath.b4xlib
    1.4 KB · Views: 6
Last edited:

hatzisn

Expert
Licensed User
Longtime User
Awesome, can you give an example of that?

The example is displayed in the code above in Button1_Click. It will show "nikos3".
 
Last edited:

hatzisn

Expert
Licensed User
Longtime User
The example is displayed in the code above in Button1_Click. It will show "nikos3".

You get the object value with the key "mylists" which is a list of lists. Then you get the item in position 2 in this list which is by itself a new list. Then you get the item in position 0 of this list which is a json object and then you get the value of key "mykey" from this json object which is "nikos3". In order to get the value according to the path you created you use it as seen in the last log().
 
Last edited:

hatzisn

Expert
Licensed User
Longtime User
You get the object value with the key "mylists" which is a list of lists. Then you get the item in position 2 in this list which is by itself a new list. Then you get the item in position 0 of this list which is a json object and then you get the value of key "mykey" from this json object which is "nikos3". In order to get the value according to the path you created you use it as seen in the last log().

To set directly the path, see first the Log(jsp.JSONPath) and then you can set the same variable to a new path following the same rules.
 
Top