This 2021, I have been reviewing for internal use Reportserver 3.3.0 with Postgresql datasource, reports are fine. That is until I came across tables that contain JSON.
How do I extract JSON objects for reports? The TYPE was recognised as VARCHAR and not JSON
Example of a JSON data, how do I extract only the value for “name”?
I have tried this SQL QUERY but got the error as below;
SELECT p.product → ‘name’ FROM products AS p
Query could not be executed: ERROR: operator does not exist: character varying → unknown Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 224
is this a valid postgres query (sending it directly, without ReportServer)? SELECT p.product → ‘name’ FROM products AS p
can you pls post a screenshot of the results?
and if you put this query into a dynamic list, what do you see exactly? can you pls post the complete error stack ?
SELECT name, jsonb_agg(t->‘name’) as brand, jsonb_agg(t->‘value’) as code FROM products, jsonb_array_elements(products.product) t
group by name ;
The results
name
brand
code
Apple Pie
[“Apple Pie”]
[“1All-1-1000-xzp”]
Orange bod
[“Orange Gala”]
[“1Bll-1-99-aaa”]
Chicken Pie
[“Downtown Chicken Pie”]
[“1Bll-1-201-aaa”]
When run sql in Reportserver
Error
Query could not be executed: ERROR: function jsonb_array_elements(character varying) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. Position: 653
Is there an alternative method to make that query? One problem I face, not knowing the supported Query functions for JSON in Reportserver.
Firstly, jsonb column should be filled with jsonb format, its not string. Select statement format depends, if you are using multi object or single (this is my case). Here is proper JSON format
[ {“item1”,“item2”} ]
The code you provided, without square brackets is interpreted as String.
insert into public.products (id, name, product) values (3, ‘Apple Pie’, ‘{“id”: 5, “name”: “Apple Pie”, “value”: “1All-1-1000-xzp”}’)
Hi,
Thanks for offering the ticket RS-4758, but I am not sure how to follow up with that.
But based on your input, it has provided me a path toward solution that helped me and hope it will help others. This is tested using SQL specific to postgresql database.
SELECT name, t.id as id, t.name as brand, t.value as code
FROM products p,
left join jsonb_to_recordset(p.product::jsonb) as t(id int, name text, value text)
group by name ;