在 Ruby 中,递归地从 json 字符串中删除空白项
In Ruby, recursively remove blank items from json string
我有一个 JSON 字符串,我需要从中删除所有空白值。类似于:
[{"body":"","user":"mike","id":1234567,"type":"","published_at":"2015-05-22T14:51:00-04:00","title":null,"updated_at":"2015-05-23T22:04:38-04:00","postoffice":"Testing","tags":"","variants":[{"value":"", "service":"canada post"}]}]
我考虑过检查所有元素并测试它们是否为“”,我还考虑过通过 JSON.load 加载 JSON 并让 proc 选项删除空白(虽然我是 Ruby 的新手,不知道该怎么做。
从 JSON 字符串中递归删除所有空白值的最佳方法是什么? (请注意,在此示例中,为了简化,变体只有一层深。实际上它可以有很多层深。)
为了完整起见,最终结果应如下所示:
[{"user":"mike","id":1234567,"published_at":"2015-05-22T14:51:00-04:00","title":null,"updated_at":"2015-05-23T22:04:38-04:00","postoffice":"Testing","variants":[{"service":"canada post"}]}]
(在我的情况下可以使用空值)。
require 'json'
json = JSON.parse(your_json)
json.first.reject! do |key, value|
value == ''
end
puts json.to_s
请注意,我必须将 null
更改为 nil
,以便它成为有效的 ruby 哈希。结果有点冗长,但完成了工作:
def strip_empties(json)
json.each_with_object([]) do |record, results|
record.each do |key, value|
if value.is_a? Array
results << { key => strip_empties(value) }
else
results << { key => value } unless value == ""
end
end
end
end
result = strip_empties(json)
输出 nil
但没有空字符串:
=> [{:user=>"mike"},
{:id=>1234567},
{:published_at=>"2015-05-22T14:51:00-04:00"},
{:title=>nil},
{:updated_at=>"2015-05-23T22:04:38-04:00"},
{:postoffice=>"Testing"},
{:variants=>[{:service=>"canada post"}]}]
require 'json'
JSON.load(json, proc do |a|
a.is_a?(Hash) && a.delete_if do |_k,v|
next unless v.is_a?(String)
v.empty?
end
end
结果:
[{"user"=>"mike",
"id"=>1234567,
"published_at"=>"2015-05-22T14:51:00-04:00",
"title"=>nil,
"updated_at"=>"2015-05-23T22:04:38-04:00",
"postoffice"=>"Testing",
"variants"=>[{"service"=>"canada post"}]}]
我有一个 JSON 字符串,我需要从中删除所有空白值。类似于:
[{"body":"","user":"mike","id":1234567,"type":"","published_at":"2015-05-22T14:51:00-04:00","title":null,"updated_at":"2015-05-23T22:04:38-04:00","postoffice":"Testing","tags":"","variants":[{"value":"", "service":"canada post"}]}]
我考虑过检查所有元素并测试它们是否为“”,我还考虑过通过 JSON.load 加载 JSON 并让 proc 选项删除空白(虽然我是 Ruby 的新手,不知道该怎么做。
从 JSON 字符串中递归删除所有空白值的最佳方法是什么? (请注意,在此示例中,为了简化,变体只有一层深。实际上它可以有很多层深。)
为了完整起见,最终结果应如下所示:
[{"user":"mike","id":1234567,"published_at":"2015-05-22T14:51:00-04:00","title":null,"updated_at":"2015-05-23T22:04:38-04:00","postoffice":"Testing","variants":[{"service":"canada post"}]}]
(在我的情况下可以使用空值)。
require 'json'
json = JSON.parse(your_json)
json.first.reject! do |key, value|
value == ''
end
puts json.to_s
请注意,我必须将 null
更改为 nil
,以便它成为有效的 ruby 哈希。结果有点冗长,但完成了工作:
def strip_empties(json)
json.each_with_object([]) do |record, results|
record.each do |key, value|
if value.is_a? Array
results << { key => strip_empties(value) }
else
results << { key => value } unless value == ""
end
end
end
end
result = strip_empties(json)
输出 nil
但没有空字符串:
=> [{:user=>"mike"},
{:id=>1234567},
{:published_at=>"2015-05-22T14:51:00-04:00"},
{:title=>nil},
{:updated_at=>"2015-05-23T22:04:38-04:00"},
{:postoffice=>"Testing"},
{:variants=>[{:service=>"canada post"}]}]
require 'json'
JSON.load(json, proc do |a|
a.is_a?(Hash) && a.delete_if do |_k,v|
next unless v.is_a?(String)
v.empty?
end
end
结果:
[{"user"=>"mike",
"id"=>1234567,
"published_at"=>"2015-05-22T14:51:00-04:00",
"title"=>nil,
"updated_at"=>"2015-05-23T22:04:38-04:00",
"postoffice"=>"Testing",
"variants"=>[{"service"=>"canada post"}]}]