Home > OS >  how to make sure that a jsonb object is not an encoded string?
how to make sure that a jsonb object is not an encoded string?

Time:08-15

I have found sometimes a jsonb object:

{"a": 1, "b": 2}

will get re-encoded and stored as a jsonb string:

"{\"a\": 1, \"b\": 2}"

is there a way to write a function in plpgsql that will reparse the string when input is not a jsonb object?

CodePudding user response:

The #>> operator (Extracts JSON sub-object at the specified path as text) does the job:

select ('"{\"a\": 1, \"b\": 2}"'::jsonb #>> '{}')::jsonb

Read about JSON Functions and Operators in the docs.

  • Related