# How to map Posgres json/jsonb type

**URL:** <https://discourse.julialang.org/t/how-to-map-posgres-json-jsonb-type/52831>\
**Category:** New to Julia\
**Tags:** postgresql\
**Created:** [January 4, 2021, 4:28pm UTC](https://discourse.julialang.org/t/how-to-map-posgres-json-jsonb-type/52831 "2021-01-04T16:28:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [January 4, 2021, 4:28pm UTC](https://discourse.julialang.org/t/how-to-map-posgres-json-jsonb-type/52831/1 "2021-01-04T16:28:50Z")

</div>

I am quite new to Julia and struggling with quite simple task.

mutable struct XYZ  
id::Int64  
item::JSON3.Object  
end

To that structure I am trying to add default/empty constructor (setting item to empty JSON3.Object):

XYZ()=XYZ(0, {})

That gives me an error:

ERROR: LoadError: LoadError: syntax: { } vector syntax is discontinued around myfile.jl line n

I could work around it this way:

str=“”“{}”“”  
emptyJson=JSON3.read(str)  
XYZ()=XYZ(0, emptyJson)

but that seems quite ugly to me. Moreover, line  
emptyJson=JSON3.read(str)  
prints {} in the output, so I should be able to use XYZ()=XYZ(0, {}).  
I don’t understand why it doesn’t work and what would be the best code for what I am trying to achieve.

Thank you.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 4, 2021, 4:36pm UTC](https://discourse.julialang.org/t/how-to-map-posgres-json-jsonb-type/52831/2 "2021-01-04T16:36:45Z")

</div>

JSON3.Object seems to be read only. What do you try to do?

> The `JSON3.Object` supports the `AbstactDict` interface, but is read-only (it represents a _view_ into the JSON string input),

[https://quinnj.github.io/JSON3.jl/stable/#Builtin-types](https://quinnj.github.io/JSON3.jl/stable/#Builtin-types)

---

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [January 4, 2021, 5:02pm UTC](https://discourse.julialang.org/t/how-to-map-posgres-json-jsonb-type/52831/3 "2021-01-04T17:02:44Z")

</div>

I have created simple REST microservice with GET/POST/DELETE. Structure XYZ represents structure of record in a database table.

Everything worked (and it was able to get/add/delete record from Postgres table) until I had to set 1 of the columns to Postgres type jsonb.

So I added that item “item” in the struct XYZ (my structure actually has more items/columns in the table than shown here, but those are now irrelevant).

And I am able to execute POST request successfully when I use that trick

```julia
emptyJson=JSON3.read(str)
XYZ()=XYZ(0, emptyJson)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 4, 2021, 5:29pm UTC](https://discourse.julialang.org/t/how-to-map-posgres-json-jsonb-type/52831/4 "2021-01-04T17:29:14Z")

</div>

So the real task is to map a Postgres `jsonb` type to something appropriate in Julia. For me it looks like to be String as the best match.  
How about something like:

```julia
mutable struct XYZ
    id::Int64
    json::String
    json_parsed::JSON3.Object
    function XYZ( id::Int64, json::String )
        new(id, json, JSON3.read(json) )
    end
end

```

So, I suggest, that your XYZ struct and some other needed methods do the translation between the Postgres types `json` and/or `jsonb`. Does this makes sense to you? I am a bit brief in my explanation, I know, but I am not sure that I have hit the real problem you have. Perhaps someone else has solved this already.  
If I am right, you may consider to change the headline, so it is clear, that it is about Postgres json/jsonb mapping to Julia.

---

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [January 5, 2021, 10:06am UTC](https://discourse.julialang.org/t/how-to-map-posgres-json-jsonb-type/52831/5 "2021-01-05T10:06:07Z")

</div>

Thank you
