# Why does Julia not support JSON syntax to create a Dict?

**URL:** <https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873>\
**Category:** Internals & Design\
**Tags:** json\
**Created:** [July 10, 2020, 9:32pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873 "2020-07-10T21:32:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![xareelee](https://avatars.discourse-cdn.com/v4/letter/x/74df32/32.png) [@xareelee](https://discourse.julialang.org/u/xareelee)\
**Post date:** [July 10, 2020, 9:32pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/1 "2020-07-10T21:32:04Z")

</div>

JSON syntax for creating Dict/Array is really handy, especially in web development. It’s useful and high productivity when you copy data and paste to code from Stackoverflow, MongoDB or any JSON string.

Just curious, why do some new languages like Julia not support JSON syntax?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 10, 2020, 9:36pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/2 "2020-07-10T21:36:00Z")

</div>

Do you mean like

```julia
D = Dict(
    :x => 1,
    :y => 2
)

```

??

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 10, 2020, 9:38pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/3 "2020-07-10T21:38:23Z")

</div>

Presumably because JSON syntax is not equivalent to Julia syntax, so the parser would have somehow be special-cased for JSON in some context, which would be a pretty weird thing to do.

If you want to write JSON, just use the JSON package:

```julia
julia> JSON.parse("""
       {"a": 1, "b": null, "c": [1, 2, 3]}""")
Dict{String,Any} with 3 entries:
  "c" => Any[1, 2, 3]
  "b" => nothing
  "a" => 1

```

I’m curious what languages (other than javascript) you would say _do_ support JSON syntax? Python _kind of_ does, although it’s only superficially similar (e.g. Python allows comments while JSON does not; JSON has `null` while Python has `None`).

---

<div class="post-metadata">

**Author:** ![xareelee](https://avatars.discourse-cdn.com/v4/letter/x/74df32/32.png) [@xareelee](https://discourse.julialang.org/u/xareelee)\
**Post date:** [July 10, 2020, 11:29pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/4 "2020-07-10T23:29:46Z")

</div>

My bad. I should ask “why not simply using curly bracket to create Dict like JavaScript”.

```julia
D = {
    x: 1,
    y: 2
}

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 11, 2020, 12:35am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/5 "2020-07-11T00:35:07Z")

</div>

> **[Comparison of programming languages (associative array)](https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(associative_array)#C++)**
>
> This Comparison of programming languages (associative arrays) compares the features of associative array data structures or array-lookup processing for over 40 computer programming languages.
> The following is a comparison of associative arrays (also "mapping", "hash", and "dictionary") in various programming languages.
> Awk has built-in, language-level support for associative arrays.
> For example:

I think you would realize that, not many programming language (apart from obvious connection to web, such as Go) uses JSON syntax.

and also,

```julia
using JSON3
julia> a = JSON3.read("""
       {
           "x": 1,
           "y": 2
       }
       """)
JSON3.Object{Base.CodeUnits{UInt8,String},Array{UInt64,1}} with 2 entries:
  :x => 1
  :y => 2

julia> a[:x]
1

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 11, 2020, 12:49am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/6 "2020-07-11T00:49:08Z")

</div>

Because the Julia developers didn’t feel that dictionaries were such a special data-structure that they deserved their own special syntax. One need only skim the Julia github page to find many influential people with very strongly held, diametrically opposed visions for syntax uses. The dict thing seems to be actually one of the less controversial syntax choices made in the language.

Fortunately, julia has a quite powerful macro system, so you can whip up your own custom json-like dict syntax in a few lines:

```julia
macro dict(ex)
    @assert ex.head == :braces
    dargs = map(ex.args) do arg::Expr
        @assert arg.head == :call
        @assert arg.args[1] == :(:)
        @assert length(arg.args) == 3
        :($(arg.args[2]) => $(arg.args[3]))
    end
    esc(:(Dict($(dargs...))))
end

@dict{
    :x : 1,
    :y : 2
}

#+RESULTS:
: Dict{Symbol,Int64} with 2 entries:
: :y => 2
: :x => 1

```

```julia
@dict{
    "a" : 1,
    "b" : @dict{
        "c" : 2,
        "d" : 3
    }
}

#+RESULTS:
: Dict{String,Any} with 2 entries:
: "b" => Dict("c"=>2,"d"=>3)
: "a" => 1

```

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [July 11, 2020, 12:56am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/7 "2020-07-11T00:56:21Z")

</div>

X-ref: [https://github.com/JuliaLang/julia/issues/8470](https://github.com/JuliaLang/julia/issues/8470)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 11, 2020, 3:21am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/8 "2020-07-11T03:21:15Z")

</div>

I think in some ways the reason for this choice is deeper. You’ll notice that the two examples you gave of this syntax are javascript and python. I think it’s not a coincidence that this is a decision that is more commonly made by slow languages than fast ones.

If you only have 1 fast datastructure you need a fast dictionary since it can semi-efficiently do everything that arrays, sets, and classes can (in python, most classes are just dicts with oop syntax). This means dicts are absolutely critical in slow languages. They are written in the fast language that the interpreter is written in, so the efficiency loss from not using a dedicated tool is made up for by the fact that you get a little bit of C powering it. In a faster language like julia (or Java or C++), a dict is just another data-structure. As such, fast languages tend to not give dictionaries special syntax.

---

<div class="post-metadata">

**Author:** ![xareelee](https://avatars.discourse-cdn.com/v4/letter/x/74df32/32.png) [@xareelee](https://discourse.julialang.org/u/xareelee)\
**Post date:** [July 11, 2020, 5:51am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/9 "2020-07-11T05:51:12Z")

</div>

Using JSON parser is not ideal. It lacks compile-time check for syntaxes and adds unnecessary runtime parsing to slow down the app.

Data Scientists focus on how to deal with Data Frames, but Web Developers focus on how to deal with Data Structures. JSON is almost standard on web.

Using `Dict()` syntax to create a dictionary is lower productivity and lower readability for complex data structures. Just imagine how to write Dictionaries in Dictionaries in Dictionaries…

Machines are getting cheaper and manpower is getting more expensive. In business world, especially for startups, the cost of development and maintenance is more important in most cases. Productive should be over performance.

If Julia is greedy enough, it should not only focus on the application of data science. One day, we might need clients and servers for training AI.

That’ my thought.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 11, 2020, 6:00am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/10 "2020-07-11T06:00:16Z")

</div>

> [@xareelee](#):
>
> Using JSON parser is not ideal. It lacks compile-time check for syntaxes and adds unnecessary runtime parsing to slow down the app.

Not true. String macros operate at compile time.  
Also, json is no where near as expressive as dict and you’ll have to pay a higher price from the abstract type.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 11, 2020, 6:35am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/11 "2020-07-11T06:35:07Z")

</div>

> [@xareelee](#):
>
> Using `Dict()` syntax to create a dictionary is lower productivity and lower readability for complex data structures. Just imagine how to write Dictionaries in Dictionaries in Dictionaries…

Precisely, which is why you should just store your data in a JSON file and read it in with one of the libraries. Storing large, complex nested `Dict`s in code (as opposed to data) may make little sense anyway — putting large, complex, nontrivial _data_ in _code_ is bad practice.

> [@xareelee](#):
>
> One day, we might need clients and servers for training AI.

I am sorry but I don’t understand what this means, or how it relates to JSON syntax.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 11, 2020, 7:47am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/12 "2020-07-11T07:47:44Z")

</div>

Also, since it wasn’t mentioned in this topic: [named tuples](https://docs.julialang.org/en/v1/manual/types/#Named-Tuple-Types-1) are another solution in Julia:

```julia
(a = 1, b = 2, c = 1:3, d = (e = 'Γ', f = 5.0))

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 11, 2020, 12:59pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/13 "2020-07-11T12:59:31Z")

</div>

I thought we’re talking about convenient copy pasta action here, if you’re serious about the data you would have it in a .json file anyways.

if you’re just copy pasting, you’re probably using some REPL, it’s not like you can copy 1GB of JSON anyways (if you’re, you’re doing it wrong), the performance change is unnoticeable considering your fingers on keyboard and mouse time.

also, compile time or not, how can you validate a JSON string without parsing it?

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [July 11, 2020, 1:08pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/14 "2020-07-11T13:08:56Z")

</div>

> also, compile time or not, how can you validate a JSON string without parsing it?

Exactly 😉 This reminds me of this nice blog post: [Parse, don’t validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/)

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [July 11, 2020, 1:22pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/15 "2020-07-11T13:22:49Z")

</div>

> [@xareelee](#):
>
> Machines are getting cheaper and manpower is getting more expensive. In business world, especially for startups, the cost of development and maintenance is more important in most cases.

I hear people say this a lot, but I’ve never seen any data to support this claim. Presumably you’d need concrete compensation package information as well as concrete server costs to build this case, but neither of those are broadly publicized in my experience.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [July 11, 2020, 2:20pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/16 "2020-07-11T14:20:55Z")

</div>

> [@xareelee](#):
>
> I should ask “why not simply using curly bracket to create Dict like JavaScript”.
> 
> ```julia
> D = {
> x: 1,
> y: 2
> }
> 
> ```

On Julia 1.5, you can use the [`SafeREPL`](https://github.com/rfourquet/SafeREPL.jl/) package to have this syntax available at the REPL (there is also a macro for source code, but I would discourage that). I have the following setting in my “startup.jl” file, to input sets and dicts as in Python, except that `=` is used instead of `:` for dicts (because `:` is already used for ranges; but you could modify this snippet if you really want to use `:`):

```julia
julia> function make_setdict(ex)
    if all(x -> Meta.isexpr(x, :(=), 2), ex.args)
        Expr(:call, :Dict, [Expr(:call, :(=>), x.args[1], x.args[2]) for x in ex.args]...)
    else
        Expr(:call, :Set, Expr(:vect, ex.args...))
    end
end;

julia> swapliterals!(:braces => make_setdict)

julia> {1, 2, 3}
Set{Int64} with 3 elements:
  2
  3
  1

julia> {1 = 2, 3 = { "a" = 4, :b = 5}}
Dict{Int64,Any} with 2 entries:
  3 => Dict{Any,Int64}(:b=>5,"a"=>4)
  1 => 2

```

But note that the “braces” syntax is reserved by Julia for probable future use.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [July 11, 2020, 6:16pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/17 "2020-07-11T18:16:46Z")

</div>

The blog post you linked is interesting, but I think it makes a mistake when it says that this is a property of a _static_ type system. All of those things work fine in Julia, scheme, Common Lisp or Python, where the objects are dynamically typed.

A lot of what is being described there is really just _functional_ vs _imperative_ style: take in a thing, and return a new value rather than take in a thing and make sure we throw an error if it’s borken.

---

<div class="post-metadata">

**Author:** ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)\
**Post date:** [July 11, 2020, 6:53pm UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/18 "2020-07-11T18:53:04Z")

</div>

One of the great things about Julia, as highlighted above, is that if you want curly brackets to construct dicts, you can write a package to do that and it will be just as “first class” as if it were part of Base Julia.

So the answer to your question:

> I should ask “why not simply using curly bracket to create Dict like JavaScript”.

is, “You can” 🙂

---

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [July 13, 2020, 8:04am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/19 "2020-07-13T08:04:02Z")

</div>

Just my two cents…

I wrote a small package to support Javascript Literals, [JSObjectLiteral](https://github.com/davidavdav/JSObjectLiteral.jl), so that you can write in Julia:

```julia
e = "ee"
a = @js { 
  b: 10,
  c: [20, { d: "thirty", e }]
}

```

which is _almost_ like you can in javascript—it is just the string literals that have to be double-quoted instead of the more common single-quoted string in javascript.

This started out as an exercise for me trying to understand Julia macros. The package can do more tricks, like assign into a dict `@js { a, b } = dict`, and has a type `JSObject` to make deep object traversal more easy to write, like `a.c.e = "fifty"`.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [July 13, 2020, 11:24am UTC](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873/20 "2020-07-13T11:24:56Z")

</div>

> [@davidavdav](#):
>
> it is just the string literals that have to be double-quoted instead of the more common single-quoted string in javascript

How big effort it would be to support javascript syntax? I don’t need the feature, I am just interested in how macros work.

[Next page](https://discourse.julialang.org/t/why-does-julia-not-support-json-syntax-to-create-a-dict/42873.md?page=2)
