如何从json文件中收集标头

时间:2017-12-19 10:59:46

标签: ruby-on-rails ruby

我试图将json文件更改为CSV。这是我的Json:

[
  {
    "id": 0,
    "email": "colleengriffith@quintity.com",
    "tags": [
      "consectetur",
      "quis"
    ],
    "profiles": {
      "facebook": {
        "id": 0,
        "picture": "//fbcdn.com/a2244bc1-b10c-4d91-9ce8-184337c6b898.jpg"
      },
      "twitter": {
        "id": 0,
        "picture": "//twcdn.com/ad9e8cd3-3133-423e-8bbf-0602e4048c22.jpg"
      }
    }
  },
  {
    "id": 1,
    "email": "maryellengriffin@ginkle.com",
    "tags": [
      "veniam",
      "elit",
      "mollit"
    ],
     "profiles": {
       "facebook": {
         "id": 1,
         "picture": "//fbcdn.com/12e070e0-21ea-4663-97d0-46bc9c7b67a4.jpg"
       },
       "twitter": {
         "id": 1,
         "picture": "//twcdn.com/3057792f-5dfb-4c4b-86b5-cce4d6bbf7ac.jpg"
       }
     }
   }
 ]

我首先想要的是收集标题。这就是我的尝试:

json = JSON.parse(File.open("live.json").read)
headings = SortedSet.new
json.each do |hash|
  headings.merge(hash.values)
end

并返回#<SortedSet:0x007fa592128088>

然后,我试图了解发生了什么(我没有顺便说一下)但是我看到当我要求json.values时我没有嵌套值。这就是我所拥有的:

id
email
tags
profiles

预期输出为:

id, email, tags, profiles.facebook.id, profiles.facebook.picture, profiles.twitter.id, profiles.twitter.picture

所以我的问题是,有人知道如何获得这些标题吗?有没有一种简单的方法可以在不使用SortedSet的情况下获取它们?

非常感谢!

3 个答案:

答案 0 :(得分:1)

使用get_recursive_keys方法获取嵌套密钥

selectList [TableDateColumn >=. timeStart, TableDateColumn <=. timeEnd] []

然后按照以下方式添加到您的排序集中:

def get_recursive_keys(hash, nested_key=nil)
  hash.each_with_object([]) do |(k,v),keys|      
    k = "#{nested_key}.#{k}" unless nested_key.nil?
    if v.is_a? Hash
      keys.concat(get_recursive_keys(v, k))
    else
      keys << k
    end
  end
end

现在Key的标题:

json = JSON.parse(File.open("live.json").read)
headings = SortedSet.new
json.each do |hash|
  headings.merge(get_recursive_keys(hash))
end

答案 1 :(得分:0)

您可以这样做:

json = <<-END
[
  {
    "id": 0,
    "email": "colleengriffith@quintity.com",
    "tags": [
      "consectetur",
      "quis"
    ],
    "profiles": {
      "facebook": {
        "id": 0,
        "picture": "//fbcdn.com/a2244bc1-b10c-4d91-9ce8-184337c6b898.jpg"
      },
      "twitter": {
        "id": 0,
        "picture": "//twcdn.com/ad9e8cd3-3133-423e-8bbf-0602e4048c22.jpg"
      }
    }
  },
  {
    "id": 1,
    "email": "maryellengriffin@ginkle.com",
    "tags": [
      "veniam",
      "elit",
      "mollit"
    ],
     "profiles": {
       "facebook": {
         "id": 1,
         "picture": "//fbcdn.com/12e070e0-21ea-4663-97d0-46bc9c7b67a4.jpg"
       },
       "twitter": {
         "id": 1,
         "picture": "//twcdn.com/3057792f-5dfb-4c4b-86b5-cce4d6bbf7ac.jpg"
       }
     }
   }
 ]
END

CSV.generate do |csv| 
  csv << [:id, :email, :tags, :profiles] # add headers with order
  JSON.parse(json).each do |item| 
    csv << [item['id'], item['email'], item['tags'].join(','), item['profiles']] # add rows with the same order
  end
end

它会给你以下csv:

id,email,tags,profiles
0,colleengriffith@quintity.com,"consectetur,quis","{""facebook""=>{""id""=>0, ""picture""=>""//fbcdn.com/a2244bc1-b10c-4d91-9ce8-184337c6b898.jpg""}, ""twitter""=>{""id""=>0, ""picture""=>""//twcdn.com/ad9e8cd3-3133-423e-8bbf-0602e4048c22.jpg""}}"
1,maryellengriffin@ginkle.com,"veniam,elit,mollit","{""facebook""=>{""id""=>1, ""picture""=>""//fbcdn.com/12e070e0-21ea-4663-97d0-46bc9c7b67a4.jpg""}, ""twitter""=>{""id""=>1, ""picture""=>""//twcdn.com/3057792f-5dfb-4c4b-86b5-cce4d6bbf7ac.jpg""}}"

但你必须决定如何处理个人资料。

<强>更新

CSV.generate do |csv| 
  csv << ['id', 'email', 'tags', 'profiles.facebook.id', 'profiles.facebook.picture', 'profiles.twitter.id', 'profiles.twitter.picture'] 
  JSON.parse(json).each do |item| 
    csv << [item['id'], item['email'], item['tags'].join(','), item['profiles']['facebook']['id'], item['profiles']['facebook']['picture'], item['profiles']['twitter']['id'], item['profiles']['twitter']['picture']] 
  end
end

将导致

id,email,tags,profiles.facebook.id,profiles.facebook.picture,profiles.twitter.id,profiles.twitter.picture
0,colleengriffith@quintity.com,"consectetur,quis",0,//fbcdn.com/a2244bc1-b10c-4d91-9ce8-184337c6b898.jpg,0,//twcdn.com/ad9e8cd3-3133-423e-8bbf-0602e4048c22.jpg
1,maryellengriffin@ginkle.com,"veniam,elit,mollit",1,//fbcdn.com/12e070e0-21ea-4663-97d0-46bc9c7b67a4.jpg,1,//twcdn.com/3057792f-5dfb-4c4b-86b5-cce4d6bbf7ac.jpg

答案 2 :(得分:0)

对于任何嵌套级别的哈希:

get = ->(hash, prefix = nil) do
        hash.flat_map do |k, v|
          v.is_a?(Hash) ? get.(v, k) : [prefix, k].compact.join('.')
        end
      end

json.map(&get)
#⇒ array of titles in each hash
json.map(&get).first
#⇒ ["id", "email", "tags",
#   "facebook.id", "facebook.picture",
#   "twitter.id", "twitter.picture"]