API call to populate form fields returns error #197
-
Hi, all, may I say what an excellent project SQLpage is, I have been looking for a way of building a crud frontend for my database for a while and I think SQLPage is going to be great. -- This returns a JSON data structure, how do I pass this into a form.
select 'text' as component, sqlpage.exec('/www/lookup.php', 'xxxxx') as contents; I modify as per the example. select 'text' as component, value->>'status' from json_each( sqlpage.exec('/www/lookup.php', 'xxxxx') ); I get the following error.
Essentially can we use data returned from an api as JSON in SQLpage for any components. Any help greatly appreciated, of course this process may not be possible. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Actually may have worked it out for myself. Insert the record into a dedicated table first, INSERT into audience.jsontest (data) values ( sqlpage.exec( 'curl','-u','xxxxxxxxxxxxxxxx:','https://api.companieshouse.gov.uk/company/xxxxxx' )::JSON ); query the new table to pull back required values. select 'card' as component;
select data->>'company_number' as title, data->>'company_name' as description
from audience.jsontest where data->>'company_number'='xxxxxx' Display in form field. |
Beta Was this translation helpful? Give feedback.
-
Hello and welcome to sqlpage ! All sqlpage pseudo-functions are executed inside sqlpage and their results are passed to the database as text. The error message select ... from json_each( sqlpage.exec(...)::json ); If you want to reuse a value without creating an intermediate table, you can use variables in sqlpage with set target_url = 'http://example.com/api/company/' || $name;
set json_results = sqlpage.exec('curl', $target_url);
select 'card' as component;
select key as title, value as description
from json_each($json_results::json); |
Beta Was this translation helpful? Give feedback.
-
I know this is stretching the original question ... how can I loop through an array of results and present in a table.
How can I unnest an array within the json object, (below does not work) any suggestions greatly appreciated as ever.
|
Beta Was this translation helpful? Give feedback.
-
Worked it out anyway for anyone who is interested.
function json_array_elements() (would be good to know what native functions are supported, presumably this is down to the underlying lib being used ? select
max($json_result::json->>'name') as recordset,
d.elem::json->>'occupation' AS occupation,
d.elem::json->>'name' AS name
from
json_each($json_result::json) t,
json_array_elements($json_result::json->'items') d(elem)
group by 2,3; |
Beta Was this translation helpful? Give feedback.
-
Your query above (https://www.db-fiddle.com/f/nNnoFtHYyPzdJWMGqrDFid/0) can be simplified to select
$json_result::json->>'name' as recordset,
item->>'occupation' AS occupation,
item->>'name' AS name
from
json_array_elements($json_result::json->'items') as items(item) |
Beta Was this translation helpful? Give feedback.
Hello and welcome to sqlpage !
All sqlpage pseudo-functions are executed inside sqlpage and their results are passed to the database as text. The error message
function json_each(text) does not exist
comes from the fact thatsqlpage.exec(...)
is a value of typetext
and json_each expects a value of typejson
. You can cast values withIf you want to reuse a value without creating an intermediate table, you can use variables in sqlpage with
set
: