Skip to content
Advertisement

SQL request to Clickhouse: select if value exist in JSON array

I’ve got a following table:

Status;Params
1;[{"Name": "Bob", "Age": 14, "Friend" : "Chuck"},{"Name" : "Alice", "Age" : 16, "Friend" : "John"}]
0;[{"Name": "Chuck", "Age": 14, "Friend" : "Bob"},{"Name" : "John", "Age" : 16, "Friend" : "Alice"}]

How can I do ‘select Status where Name XXX represented at least in one Array[i].Name’?

Advertisement

Answer

select Status
from
where arrayExists(x-> JSONExtractString(x,'Name') = 'XXX', JSONExtractArrayRaw(Params))




SELECT arrayExists(x -> (JSONExtractString(x, 'Name') = 'Alice'), JSONExtractArrayRaw(x))
FROM
(
    SELECT '[{"Name": "Bob", "Age": 14, "Friend" : "Chuck"},{"Name" : "Alice", "Age" : 16, "Friend" : "John"}]' AS x
)

┌─arrayExists(lambda(tuple(x), equals(JSONExtractString(x, 'Name'), 'Alice')), JSONExtractArrayRaw(x))─┐
│                                                                                                    1 │
└──────────────────────────────────────────────────────────────────────────────────────────────────────┘
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement