Thank you all for really good explanations. I learned a lot! I will try to summarize what I learned.
What I understood, that JSON data model (nested dicts and lists) is not first class citizen and will not become one because this is not considered as a fundamental data model worth including into Julia as language feature with dedicated syntax. It is possible to have nested dicts and lists in Julia without any third party packages, but the whole thing is far less usable compared with arrays. The only available syntactic sugar is =>
. But I don’t really understand why =>
is needed, because Dict(a = 1)
is even better.
Also I feel that name JSON data model is confusing, because people think, that I’m talking about JSON data serialization format. Maybe nested dicts and lists would be better name? I don’t know how to properly call it, maybe JON (Julia Object Notation)?
Those who work with JON (most web developers) should use third party packages and one of many options available to work with JSON data model.
Here are some options for writing JON:
Dict("a" => Dict("b" => [Dict("c" => "d")]))
Dict(:a => Dict(:b => [Dict(:c => "d")]))
Dict([(:a, Dict([(:b, "c")]))])
(a = (b = [(c = "d",)],),)
using DataStructures
OrderedDict("a" => OrderedDict("b" => [OrderedDict("c" => "d")]))
using MacroTools
@json {"a": {"b": [{"c": "d"}]}}
using MacroTools
@json {a: {b: [c: "d"]}}
using ?
json"""{"a": {"b": "c"}}"""
using ?
D(a = D(b = [D(c = "D")]))
Options for printing JON:
julia> data
Dict{String,Dict{String,Array{Dict{String,String},1}}} with 1 entry:
"a" => Dict("b"=>[Dict("c"=>"d")])
julia> repr(data)
"Dict(\"a\"=>Dict(\"b\"=>[Dict(\"c\"=>\"d\")]))"
julia> print(repr(data))
Dict("a"=>Dict("b"=>[Dict("c"=>"d")]))
julia> using JSON
julia> JSON.print(json, 2)
{
"a": {
"b": [
{
"c": "d"
}
]
}
}
julia> using ?
julia> @ishow json
▾ a
▾ b
▾ 0
▾ c
"d"
Personally I would prefer to have only one best option built into Julia in a similar fashion as with arrays, but at least now I know that using one of many third party packages is the way to work with JON.
For me as a web developer this is a bit disappointing, but I think one of third party packages can fix it, not sure which one yet.
And here is how I would like it implemented:
julia> data = {"a": "b": [{"c": "d"}]}
julia> {data..., "x": [1, 2, 3]}
Dict{Any: Any}
a:
b:
- c: d
x: [1, 2, 3]
julia> @json data
{
"a": {
"b": [
{
"c": "d"
}
]
}
}
julia> @json data > filename.json
julia> {i: i^2 for i=1:9 if isodd(i)}