Home > Blockchain >  Querying a JSON array in rails psql Query
Querying a JSON array in rails psql Query

Time:10-17

I have a JSON a={cake: ["a","b","c","d"]} I need to query any value in a given values are present in the array Like if the input is ["a","x"] output should be true

if input is ["c","d"] then output is true

if input is ["x","z"] the n output is false

CodePudding user response:

Use intersection (&) and check if any of input is in the original array:

2.6.6 :002 > (a[:cake] & ['a','x']).any?
 => true 

2.6.6 :003 > (a[:cake] & ['x','z']).any?
 => false 

CodePudding user response:

simply try to find common between json array and input array.

(a[:cake] & input_array).any?
  • Related