PostgresSQL add elements

0

I need to form the json in this way in PostgreSQL.

ubi_json json;

  FOR i IN 0..7 LOOP

    ubi_json = ubi_json || to_json('valor',i);

  END LOOP;

Error comes out:

Error : no existe la función to_json(unknown, integer)

I need this:

[
    { "valor": 1 },
    { "valor": 2 },
    { "valor": 3 },
    { "valor": 4 },
    { "valor": 5 },
    { "valor": 6 },
    { "valor": 7 },
    { "valor": 8 },
    { "valor": 9 },
    { "valor": 10 }
]
    
asked by Vinicio Gomez 06.08.2017 в 17:41
source

1 answer

0

Well I've never tried to concatenate the json in that way (I do not know if it can be). I recommend you to work that variable "ubi_json" in TEXT and you are assembling your json, and when you have armed the casteas "ubi_json :: JSON"

ubi_json JSON;
tmp_json text := '[';
FOR i IN 0..7 LOOP
    tmp_json = tmp_json || '{"valor":' || i::text || '},'
END LOOP;

SELECT (RTRIM(tmp_json, ',') || ']')::JSON INTO ubi_json;
    
answered by 08.08.2017 / 00:52
source