How to extract the values from json input in postgresql?

Naveenkumar K

I am working with the PostgreSQL stored function, I need to extract the values from the JSON input and match with the column in the table and return the table in JSON format. My JSON input,

{
"sensor":"sensor1",
"fromdate":date,
"todate":"date
}

my sensortable here is the function I worked so far

select array_to_json(array_agg(row_to_json(d)))
      from (
        select sensor,id,value,created_date
        from probe_data
        where  probe_data.sensor =sensors
         AND probe_data.created_date >=fromdate AND  probe_data.created_date <= todate
      ) d
select x.sensores,x.fromdate,x.todate from json_to_recordset($1) x
(
sensors text,
fromdate timestamp,
todate timestamp)

As you can see, I can able to get the data from JSON input but I don't know how to access it inside the WHERE condition. I need some help to do this.

Akhilesh Mishra

simply you can use JSONB operators to achieve this like below:

Try this:

create or replace function example (param1 jsonb) 
returns table (sensor_ varchar, id_ int, value_ numeric, created_date_ TIMESTAMP) 
as 
$body$
    begin
    RETURN query
    select sensor, id, value, created_date from probe_data 
    where 
    created_date >=(param1->>'fromdate')::TIMESTAMP 
    and 
    created_date <= (param1->>'todate')::TIMESTAMP;

    end;

$body$ 

LANGUAGE plpgsql;

if you want the above output in JSON ARRAY format then

Try This:

create or replace function example1 (param1 jsonb) 
returns jsonb 
as 
$body$
declare 
    jsondata jsonb;

begin
    
    jsondata = (select 
            array_to_json(array_agg(row_to_json(d))) from (
            select sensor, id, value, created_date from probe_data 
            where 
            created_date >=(param1->>'fromdate')::TIMESTAMP 
            and 
            created_date <= (param1->>'todate')::TIMESTAMP ) d);
RETURN jsondata;
end;
$body$
LANGUAGE plpgsql;

FIDDLE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Postgresql - Extract values from json from [] values

How to extract values from array json column into multiple rows in Postgresql?

Extract values from array of json elements in Postgresql

How to extract json values from json in javascript

How to extract those values from the JSON string

How to extract values from JSON file in java?

How to extract values from json string?

How to extract and count values from a nested JSON?

How to extract values from XML in PostgreSQL PL/pgSQL?

How to extract from input?

How to extract a certain string and delete rest of values from an input string?

How to parse/extract specific values from an input huge file in python?

How to extract values from HTML <input type="date"> using jQuery

how to extract numeric values from input string in java

How to extract the values from request input xml in jmeter

How to remove the null values from the json input

How to update values from a JSON input

How extract data from json to table (dynamic) in postgresql

How to use json array to extract different values from JSON document?

Extract values from complex JSON

Extract values from json in javascript

Extract values from JSON array

JSON extract values from key

Extract values from JSON in Oracle

How to use jq to extract variable keys and their values from JSON?

How to extract specific values from JSON response in Postman and concatenate

How to extract values from JSON-encoded column?

How to extract values from a String that cannot be converted to Json

How to extract the values from a Json Array in ballerina 0.982.0