1

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?

1
  • 2
    Why would you want to insert bytes instead of text in such a column? Can you post the code you're using (esp. the part that fails)? Commented Dec 5, 2013 at 13:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

when you say JSON allows 3 data types, are you referring to the values in a JSON object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.