[!TIP] Please install CJSON plugin for language support in visual studio code
One of the strong feature which we have introduced is importing CJson/Json files in another CJson file.
It works in the similar way, how we import libraries in programming languages. All you need to import it using
$import "path/to/target/file"
and deserialize
the file.
Unlike XPATH for XML files, we have JPATH for JSON files. But as the drawback of plain data files, we cannot refer any variable inside a json object to another variable. This feature is useful when you have to duplicate the json key, but the value will be pulled from another variable which is already present in the json file.
You can also refer to a variable which will be loaded after importing the file.
You can inject a variable dynamically also. Instead of replacing a variable value by parsing as gson
object, put a key in format <keyToBeReplaced>
like below:
{
"idValue": <id>
}
Now create a HashMap
with key as <id>
and store relevant value in it.
While invoking inject
function, pass the HashMap as the second parameter.
Injection can be performed single or bulk. Lets look at each one below:
Single injection can be performed by providing key and value to the inject()
.
inject(String key, Object value)
This function returns deserialized
class object.
Bulk injection can be performed by storing the injection data in Dictionary
or HashMap
where key is the key
where injection need to be performed and value
is the value.
inject(HashMap<String, Object>)
This function returns deserialized
class object.
CJSON also supports commented lines by adding //
at the start of the line.
Please note, inline comments are not supported. Contributers create an issue here
Parsing of CJSON string content is also possible now. You can create CJSON object with a second parameter(specific to language). For language specific details, refer below.
deserialize
is a function where CJSON content is compiled and converted into equivalent JSON content.
Now, users can consume this content as a deserailized class object like any other parser works.
Refer to example for language specific syntax.
User can consume converted JSON content to String
content too. Every language has a function deserializeAsString()
which converts compiles and converts CJSON to JSON content and returns JSON content in string format
Any JSON key value pair can be removed by providing its JPath.
Please Note, if you use remove
before deserialize
you will receive UndeserializedCJSON
exception.
This is because, unless the content is deserialized, CJSON engine has not processed the CJSON content.
This function returns deserialized class object post removal.
Here are the examples
Single removal can be performed by simply providing the JPath as parameters.
Bulk removal accepts List<String> JPaths
to perform removal.
Class Object
to JSON String
Any Java object can be converted to json string by using toString()
function.
It accepts pure java object and returns JSON in string.
Here are the examples
This function finds the value of the provided JPath as parameter. This function returns Object
type data.
So a type cast will be required to unlock native functions.
Returns all possible JPaths
inside JSON.
Returns all values to all possible JPaths
inside JSON. Before executing this function, you need call for getting all possible keys function.
This function validates whether the provided string is JSON content or not.
Unline XPath in xml format, JSON has JPath format. THe difference is, instead of //
or /
, JPath starts with $.
Below is an example:
$.path.to.variables
If any array is encountered, below is the way index is provided:
$.path.to.array[index].variable
Keywords | Description |
---|---|
$import |
To import other json file |
$.jpath |
Refer to a local variable inside JSON |
Comments(Single/ Multi-line) |
// |
<key> |
Expects a dynamic variable |