# Error writing down a JSON file with JSON.jl

**URL:** https://discourse.julialang.org/t/error-writing-down-a-json-file-with-json-jl/106028
**Category:** Data
**Tags:** json, data, file
**Created:** [November 10, 2023, 11:34am UTC](https://discourse.julialang.org/t/error-writing-down-a-json-file-with-json-jl/106028 "2023-11-10T11:34:26Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![PatrickeTownsend](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patricketownsend/32/50028_2.png) [@PatrickeTownsend](https://discourse.julialang.org/u/PatrickeTownsend)
#### Post date: [November 10, 2023, 11:34am UTC](https://discourse.julialang.org/t/error-writing-down-a-json-file-with-json-jl/106028/1 "2023-11-10T11:34:26Z")

</div>

Hello,  
I’m trying to export some data to a JSON format, the file is succesfully created with this function:

```julia
function writeJSON()
    r = rand(3,3)
    N=3
    data = [Dict("x"=> r[i,1],"y"=> r[i,2],"z"=>r[i,3]) for i=1:N]
   
    open("foo.json","w") do f
       JSON.print(f, data, 4)
    end
    
end

```

but in the resulting `foo.json` the “y” and “z” values are inverted, i’m not sure why, it should look like this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/c/4ccf9723d8ab1247cced021d9829da7d2f73dc7e.png)

And the file generated is this:

```julia
[
    {
        "x": 0.8212211605554455,
        "z": 0.5338611788222072,
        "y": -0.20146475424383917
    },
    {
        "x": -0.5412324524725539,
        "z": 0.4710378620065893,
        "y": 0.696556361644057
    },
    {
        "x": -0.5595207932869602,
        "z": -0.2356000256849917,
        "y": 0.7946251378963304
    }, .....

```

How can i fix it?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 10, 2023, 12:33pm UTC](https://discourse.julialang.org/t/error-writing-down-a-json-file-with-json-jl/106028/2 "2023-11-10T12:33:52Z")

</div>

That has nothing to do with JSON, `Dict`s are unordered:

```julia
julia> Dict("x"=> r[1,1],"y"=> r[2,2],"z"=>r[3,3])
Dict{String, Float64} with 3 entries:
  "x" => 0.993151
  "z" => 0.0282255
  "y" => 0.29912

```

If you need to rely on the order use an `OrderedDict` from [`OrderedCollections`](https://juliacollections.github.io/OrderedCollections.jl/latest/ordered_containers.html) or another structure which preserves order.
