Search code examples
jsonsiddhi

Siddhi - How to generate a JSON string


I'm trying to generate a JSON string by combining various columns and save the JSON into Postgres table having JSON datatype. From the documentation, it is clear about reading feom JSON string.

define stream InputStream(json string);
from InputStream
select json:getString(json,"$.name") as name
insert into OutputStream;

But can we build the JSON in-flight and insert into table? Something like...

select '{"myname":json:getString(json,"$.name")}' as nameJSON
insert into postgresDB

Where nameJSON will be a JSON datatype in Postgres.

Any help would be greatly appreciated.


Solution

  • You can use, JSON:setElement to create a JSON from the attributes

    from OutputStream
    select json:setElement("{}", "$", json:getString(json,"$.name"), "myname") as value
    insert into TempStream;