Search over a nested / multi-level Postgres JSON type using Active Record

后端 未结 1 1913
不知归路
不知归路 2020-12-16 20:05

Suppose I have the json object below

:response_body => {
    \"status\" => \"ok\",
    \"um\" => {
        \"a\" => \"a\",
        \"b\" => {
         


        
相关标签:
1条回答
  • 2020-12-16 20:45

    If you are using the Postgres as database and the column really is a json type, you can use a search in Rails like this:

    LogService.where("response_body#>>'{status}' = 'ok'")
    

    This #>> will walk over the json path and make the search. If you use only #> (with just one >), it will return the remaing json.

    Another example, if you want to get the "c" => "ok"

    LogService.where("response_body#>>'{um, b, c}' = 'ok'")
    

    I think that´s it.

    0 讨论(0)
提交回复
热议问题