Skip to content
Discussion options

You must be logged in to vote

I ended up doing this on the Postgres side, using Postgres to disassemble and reassemble the JSON.

INSERT INTO agents (username, favorite_thing, favorite_thing_payload)
SELECT 
    username,
    favorite_thing_json ->> 'tag' AS favorite_thing,
    favorite_thing_json -> 'payload' AS favorite_thing_payload
FROM (SELECT $1 AS username, $2::JSONB as favorite_thing_json);
SELECT
    username
    JSONB_STRIP_NULLS(
        JSONB_BUILD_OBJECT(
            'tag', favorite_thing,
            'payload', favorite_thing_payload)) AS "favorite_thing!: Json<ThingKind>"
FROM
    agents;

And that works great with what serde expects.

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@inklesspen
Comment options

@sunnypatell
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by inklesspen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants