The JSONPath Object
JSONPath
JSONTools.JSONPath
— TypeJSONPath is used to access and set the data in JSON serializable objects using indexing. Below is the list of propertie of the JSONPath object:
Properties that define the path
path
- A Tuple of keys and indices that defines the path to the value.
Properties that spefify how to handle inserting data
parents
- If true, the path will be created if it doesn't exist.candestroy
- If true, the path will be overwritten if it exists (including the parent values that are in the way).canfilllists
- If true, the path will be created if it doesn't exist and the lists will be filled with the listfiller function.listfiller
- This function is used to fill the lists when canfilllists is true.
JSONPaths are created using variadic arguments, so you don't need to put your list of keys into a container. JSONPaths can be used for getting and setting the values in a JSON structure.
julia> myjson = Dict()
Dict{Any, Any}()
julia> mypath = JSONPath("a", "b", "c", parents=true, candestroy=true)
Main.JSONTools.JSONPath(("a", "b", "c"), true, true, false, Main.JSONTools.var"#3#6"())
julia> myjson[mypath]
MISSING_KEY::EndOfPath = 0
julia> myjson[mypath] = 1
1
julia> myjson
Dict{Any, Any} with 1 entry: "a" => Dict{Any, Any}("b"=>Dict{Any, Any}("c"=>1))
EndOfPath
JSONTools.EndOfPath
— TypeEndOfPath represents an error that occured during access to a JSON path. EndOfPath is not an exception. It's just an enum with a few values which may be useful for further processing.
Fields
MISSING_KEY
- The requested key of an object is missing.OUT_OF_BOUNDS
- The requested index of a list is out of bounds.NOT_A_CONTAINER
- Requested access to an object which is not an object or a list.INVALID_KEY_TYPE
- Requested access to an object using index or to a list using a key.INVALID_ROOT
- The root object is not a Dict or a Vector. The errors that occure in the root object are separated because thesetindex!
function isn't able to modify the root object so the user might want to handle them in a different way.
Example:
include("../../src/JSONTools.jl") # hide
using .JSONTools # hide
import JSON
data = JSON.parse("""
{
"a": {"b": {"c": 1}},
"b": [1, 2, 3],
"c": {"d": 1}
}""")
nothing # hide
julia> data[JSONPath("a", "b", "d")]
ERROR: UndefVarError: `data` not defined
julia> data[JSONPath("b", 4)]
ERROR: UndefVarError: `data` not defined
julia> data[JSONPath("c", "d", "e")]
ERROR: UndefVarError: `data` not defined
julia> data[JSONPath(1, "b")]
ERROR: UndefVarError: `data` not defined
julia> data1 = 1
1
julia> data1[JSONPath("a", "b", "c")]
INVALID_ROOT::EndOfPath = 4