yoavba
yoavba

Reputation: 411

Insert bytea into postgresql json type

I am trying with no success to insert a byte array in a postgresql json type as one of the json columns. is it possible? does anyone have an example?

Upvotes: 1

Views: 4898

Answers (1)

Chris Travers
Chris Travers

Reputation: 26464

You can't, at least natively. JSON only allows 3 basic primative data types: number, string, and boolean. Everything else must be serialized to a string or number.

This means that you have three basic options:

  1. Serialize to hexadecimal. The advantage is that it becomes easy to turn into a bytea in PostgreSQL if you need it.

  2. Serialize to base64. The advantage here is that it uses up less space.

  3. Serialize to a number array. This is not preferred in my view since it is hard to constrain each number to between 0 and 255.

Upvotes: 1

Related Questions