JSON and Postgres Variable Queries - Mailing list pgsql-hackers

From Joey Caughey
Subject JSON and Postgres Variable Queries
Date
Msg-id 81724D29-A50F-4F77-A2F4-491FB7D39E9B@parrotmarketing.com
Whole thread Raw
Responses Re: JSON and Postgres Variable Queries  (Andrew Dunstan <andrew@dunslane.net>)
Re: JSON and Postgres Variable Queries  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
I’m having an issue with JSON requests in Postgres and was wondering if anyone had an answer.

I have an orders table with a field called “json_data”.

In the json data there is a plan’s array with an id value in them.
{ "plan”: { “id”: “1” } } }

I can do regular queries that will work, like so:
SELECT json_data->>’plan'->>’id' as plan_id FROM orders;

But if I try to query on the data that is returned it will fail:
SELECT json_data->>’plan'->>’id' as plan_id FROM orders WHERE plan_id = 1;
OR
SELECT json_data->>’plan'->>’id' as plan_id FROM orders GROUP BY plan_id;
OR
SELECT json_data->>’plan'->>’id' as plan_id FROM orders ORDER BY plan_id;

Is this something that has been overlooked? or is there another way to go about this?

I’ve tried everything from the documentation here:

I’ve attached a json dump of the orders table.

Thanks in advance,

Joey










Attachment

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Shouldn't pg_(sh)seclabel.provider be marked NOT NULL?
Next
From: Andrew Dunstan
Date:
Subject: Re: JSON and Postgres Variable Queries