# How to write data to file with JSON.jl?

**URL:** <https://discourse.julialang.org/t/how-to-write-data-to-file-with-json-jl/2993>\
**Category:** Data\
**Tags:** question, json\
**Created:** [March 31, 2017, 7:03pm UTC](https://discourse.julialang.org/t/how-to-write-data-to-file-with-json-jl/2993 "2017-03-31T19:03:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 31, 2017, 7:03pm UTC](https://discourse.julialang.org/t/how-to-write-data-to-file-with-json-jl/2993/1 "2017-03-31T19:03:11Z")

</div>

Consider the following example:

```julia
using JSON

people = [Dict("name"=>"CoolGuy", "company"=>"tech") for i=1:1000]
companies = [Dict("name"=>"CoolTech", "address"=>"Bay Area") for i=1:100]

data = Dict("people"=>people, "companies"=>companies)
json_string = JSON.json(data)

open("foo.json","w") do f
  JSON.print(f, json_string)
end

```

This script generates a JSON file with a bunch of extra `"` marks, without space and newlines. How to print the output correctly, and preferably in a more readable format?

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [March 31, 2017, 7:39pm UTC](https://discourse.julialang.org/t/how-to-write-data-to-file-with-json-jl/2993/2 "2017-03-31T19:39:39Z")

</div>

You take data, transform it into a string (JSON formatted) and then transform this _string_ into a quoted string when writing to a file. Instead, you should format only once, either with:

```julia
open("foo.json","w") do f 
    write(f, json_string) 
end

```

or with:

```julia
open("foo.json","w") do f
    JSON.print(f, data)
end

```

As for formatting, I’d just use a browser extension (e.g. [for Google Chrome](https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh)) or editor mode to see the content formatted.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 31, 2017, 7:40pm UTC](https://discourse.julialang.org/t/how-to-write-data-to-file-with-json-jl/2993/3 "2017-03-31T19:40:58Z")

</div>

I was just typing this finding, thanks for the answer @dfdx 🙂

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [April 1, 2017, 1:50am UTC](https://discourse.julialang.org/t/how-to-write-data-to-file-with-json-jl/2993/4 "2017-04-01T01:50:43Z")

</div>

JSON supports pretty printing. Just pass a third argument with the number of spaces to indent by.

```julia
julia> JSON.print(STDOUT, Dict("julia" => "0.5"), 4)
{
    "julia": "0.5"
}

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 1, 2017, 5:29pm UTC](https://discourse.julialang.org/t/how-to-write-data-to-file-with-json-jl/2993/5 "2017-04-01T17:29:18Z")

</div>

Thanks @fengyang.wang, I am using pretty printing now.
