How to map Posgres json/jsonb type

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

mutable struct XYZ

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:

XYZ()=XYZ(0, emptyJson)

but that seems quite ugly to me. Moreover, line
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.

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),

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
XYZ()=XYZ(0, emptyJson)

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:

mutable struct XYZ
    function XYZ( id::Int64, json::String )
        new(id, json, )

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.

Thank you

1 Like