1

I have table with columns :

id, word, decode

where decode is a json array like this:

[{
    "character": "三",
    "components": [
        "一",
        "二"
    ]
},
{
    "character": "大",
    "components": [
        "大"
    ]
},
{
    "character": "洋",
    "components": [
        "氵",
        "羊"
    ]
}]

I need search components in components. For example '二'

1
  • one more example from another [ { "character": "俄", "components": [ "亻", "手", "戈" ] }, { "character": "罗", "components": [ "罒", "夕" ] }, { "character": "斯", "components": [ "甘", "一", "八", "斤" ] } ] Commented Sep 8, 2019 at 15:22

1 Answer 1

0

you can use json_array_elements_text(json) function to expands a json array to a set of text values, and then filter by your desired value :

select q.arrays
  from
  (
    select json_array_elements_text(decode) as arrays
      from tab
  ) q
 where q.arrays like '%二%';


arrays
---------------------------------------------------
{ "character": "三", "components": [ "一",  "二" ]}

if you exactly want to extract the sub-component which contains your character("二"),then use :

select *
  from
  (
    select  elms -> 'components' as elm
      from  tab t,
            json_array_elements(decode) as elms
    )  q
  where elm::text like '%二%';   

elm
---------------------------------------------------
[ "一",  "二" ]

Demo

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

Comments

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.