3

I have a column in my table containing a JSON statuses_json:

{
    "demoStatus" : "true",
    "productionStatus": "false"
}

I would like to retrieve a value where the key is LIKE some string. For example, if I pass in "demo", I want to retrieve the value for the key demoStatus.

Right now I am able to retrieve values when passing the exact key:

`statuses_json->>'productionStatus' = 'false' `;
1
  • jsonb_path_query? I don't have a new enough version of postgresql to test it for you. Commented Mar 30, 2021 at 6:44

2 Answers 2

2

Extract the keys and run a query on it:

select *
from json_object_keys('{
    "demoStatus" : "true",
    "productionStatus": "false"
}') k where k like '%demo%';

I don't have a new enough version of postgresql but jsonb_path_query looks interesting, too. Then used statuses_json->>(...) to extract the corresponding value(s).

Sign up to request clarification or add additional context in comments.

Comments

0
select statuses_json from your_table
where statuses_json->>(
    select prop
    from json_object_keys(statuses_json) as prop
    where prop like 'demo%'
) = 'false';

1 Comment

Your inner query will produce a collection, so you cannot use an = operator there

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.