CJSon path specification

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Modules > cJSON >

CJSon path specification

 

Multiple functions in the CJSon module use the concept of path specification.

 

This feature is unique to thinBASIC and it is not present in the original CJSon.

 

Let's explain the feature on specific JSON example of the imaginary cakes.json file:

 

[

 {

         "id": 1,

         "name": "Cake for morning",

         "topping":

                 [

                         { "tid": "5001", "type": "None" },

                         { "tid": "5002", "type": "Glazed" },

                         { "tid": "5005", "type": "Sugar" },

                 ]

 },

 {

         "id": 2,

         "name": "Cake for afternoon",

         "topping":

                 [

                         { "tid": "5003", "type": "Chocolate" },

                         { "tid": "5004", "type": "Maple" }

                 ]

 }

]

 

Referencing the root

 

In most cases, you don't need to reference root explicitly, you can completely omit it.

 

In cases such as in the JSON above, where the JSON is basically array, you might need to reference the root in order to receive the number of items in the root.

 

Should that be the case, please use a simple dot as root identifier:

 

uses "cjson""console"

 

dim json as new CCJson("c:\cakes.json")         ' Create CCJson object from cakes.json

 

printl json.Path.Count(.)                       ' Number of items in root JSON array - in this case it will be 2

 

waitkey

 

 

Referencing items in the root via index

 

In order to reference the n-th item in the root, you can use 1-based index in the braces:

 

uses "cjson""console"

 

dim json as new CCJson("c:\cakes.json")         ' Create CCJson object from cakes.json

 

printl json.Path.Ptr((1))                       ' Reference first item in the root

printl json.Path.Ptr((2))                       ' Reference second item in the root

 

waitkey

 

 

Referencing items in the JSON in general

 

Every item can be referenced by index, name or name with index - depends on the JSON topology. Each of the item references is separated via dot.

 

Practical example - to reference the id fields, we can first reference the index and then the specific item. Both separated by a dot.

 

uses "cjson""console"

 

dim json as new CCJson("c:\cakes.json")         ' Create CCJson object from cakes.json

 

printl json.Path.Value((1)."id")   

printl json.Path.Value((2)."id")   

 

waitkey

 

To reference the first tid of the topping in the first cake, we can again use this approach:

 

uses "cjson""console"

 

dim json as new CCJson("c:\cakes.json")         ' Create CCJson object from cakes.json

 

printl json.Path.Value((1)."topping"(1)."tid")  ' If the array is named, you can index directly the name,

                                                ' as can be seen for the topping

                                                ' Alternatively, (1)."topping".(1)."tid" would be also accepted

 

waitkey