G
G
Gravio2019-06-28 06:46:58
PostgreSQL
Gravio, 2019-06-28 06:46:58

PostgreSQL search in json array?

The table is like this:
id, comp
comp is json:

[
  {
    "character": "俄",
    "components": [
      "亻",
      "手",
      "戈"
    ]
  },
  {
    "character": "罗",
    "components": [
      "罒",
      "夕"
    ]
  },
  {
    "character": "斯",
    "components": [
      "甘",
      "一",
      "八",
      "斤"
    ]
  }
]

You need to search for components. For example, I need to find 夕, then they will return the full string
id, comp

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-06-28
@Gravio

SELECT * FROM some_table WHERE comp @> '{"components": ["夕"]}'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question