1

I have a column of JSON type in a postgres table. It currently has values like this

{"value": "abc"}

I want to write a SQL query that can change this to

[{"value": "abc", "timestamp": 1465373673}]

The part timestamp: 1465373673 will be hard coded

Any ideas on how this SQL query can be written?

0

2 Answers 2

2

You can use json_build_array and json_build_object:

UPDATE test 
set a = json_build_array(
          json_build_object('value', a->'value', 'timestamp', 1465373673)
        );

Here's a fiddle.

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

Comments

2

Use the concatenation operator and the function jsonb_build_array():

select jsonb_build_array('{"value": "abc"}'::jsonb || '{"timestamp": 1465373673}');

              jsonb_build_array
---------------------------------------------
 [{"value": "abc", "timestamp": 1465373673}]
(1 row)

Read JSON Functions and Operators.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.