0

I want to get json out of my PostgreSQL database running version:

PostgreSQL 9.3.1 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3), 64-bit

The data I am trying to get is like:

{
  "a" : "value",
  "b" : {
        "c" : "some_vaue here"
   }
}

I am getting value from one table and some_value from some other table using join.

How can I achieve that?

Here is something similar, but I'm getting an error:

QUERY="
    SELECT row_to_json(o) FROM (
      SELECT oltl.id::text as ordinal, 
             oltl.nid as code,
             oltl.description as description, 
             SELECT row_to_json(j) 
               FROM ( SELECT cilantag.tag as code ) AS j
        from olt_languages oltl 
               inner join ci_language_tags cilantag 
                       on oltl.ci_language_tag_id=cilantag.id
    ) AS o";

The error I am getting is

ERROR: syntax error at or near "SELECT" LINE 1: ...oltl.nid as code,oltl.description as description, SELECT row... ^ ` enter code here

1 Answer 1

1

A subquery in a select list has to be enclosed in brackets, try:

SELECT row_to_json(o) 
FROM (
    SELECT 
        oltl.id::text AS ordinal, 
        oltl.nid AS code,
        oltl.description AS description, 
        (           -- added
            SELECT row_to_json(j)
            FROM (
                SELECT cilantag.tag AS code 
                ) AS j
        ) AS tags   -- + alias
    FROM olt_languages oltl 
    INNER JOIN ci_language_tags cilantag 
    ON oltl.ci_language_tag_id=cilantag.id
) AS o
Sign up to request clarification or add additional context in comments.

Comments

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.