6

I'm trying to set up a view based on a query to a text column that contains JSON in the latest version (9.3.4) of Postgres, but I get an error that I have not been able to find any discussion about.

Let's assume the table is called table1 and the particular column, json_data, has something like

{"item1": "value1", "item2": "value2", "item3": 3, "item4": 4, "item5": 5}

Here is my query:

SELECT 
json_extract_path_text((table1.json_data)::text, 
('item1'::character varying)::text) AS item1
FROM
table1

The error I get is

ERROR:  function json_extract_path_text(text, text) does not exist
LINE 2: json_extract_path_text((table1.json_data)...
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

I'm lost on how to fix this. (Also, I have a similar view that works completely fine using the same syntax on a similar text column in the particular table.)

3
  • "the latest version" = "9.3" ? 9.4 beta? Commented Jun 16, 2014 at 2:42
  • I'm using the 9.3 version. Commented Jun 16, 2014 at 2:44
  • Thanks. What's "latest" now doesn't mean "latest" a year later when someone else reads the question & answers, after all. Commented Jun 16, 2014 at 3:17

2 Answers 2

11

Since table1.json_data is already text you need to cast it to json. You also don't need to specify the table name because it is in the FROM clause.

SELECT json_extract_path_text(json_data::json,'item1') AS item1 FROM table1;
Sign up to request clarification or add additional context in comments.

Comments

5

For some reason, you're casting the json input to text:

json_extract_path_text((table1.json_data)::text

don't do that.

SELECT 
    json_extract_path_text(
        table1.json_data,
        'item1'
    ) AS item1
FROM table1

5 Comments

I still get an error, though it's different: ERROR: column table1.json_data does not exist...
Well, there's no possible way I can help you with that, as you haven't shown your table definitions. The query works fine, as you can verify by putting a literal json value in place of table1.json_data.
Your question says "Let's assume the table is called table1 and the particular column, json_data, has something like {…json…}". If table1.json_data does not exist, that's on you; this answer is correct.
how to put where clause if we want to retrieve row based on the matching of data from json_data like for case based on 'item 1'
@nikhil84 Please post a new question. Put a link in the new question back to this one for context if you want.

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.