我的表
validate :checksum_valid?
private
def checksum_valid?
if number.present?
errors.add(:number, "is not valid.") unless number_valid?
end
end
def number_valid?
number.length < 20 && Luhn.valid?(number)
end
插入:
create table i (a json)
我希望提取2541之后出现的字段。在这种情况下答案为0
查询我的Json:
insert into i values ('[[1300,[{"id":5,"time":1451423706048,"zon":0,"name":"AMighty","loc":"ny"},[[3405,0,1000],[35,0,1000],[3401,0,10000],[2541,0,1000]]]]]' )
它只返回一行。
答案 0 :(得分:2)
您需要遍历所有嵌套数组才能获得必要的元素:
WITH i(a) AS ( VALUES
('[[1300,[{"id":5,"time":1451423706048,"zon":0,"name":"AMighty","loc":"ny"},[[3405,0,1000],[35,0,1000],[3401,0,10000],[2541,0,1000]]]]]'::JSON)
)
SELECT
array_element->1->1->3 as array_you_look_for,
array_element->1->1->3->1 as inner_array_element
FROM i,
json_array_elements(a) as array_element;
输出:
array_you_look_for | inner_array_element
--------------------+---------------------
[2541,0,1000] | 0
(1 row)