从Ruby中的分层JSON生成扁平化JSON(反规范化)的最佳方法 [英] Best way to produce a flattened JSON (denormalize) out of hierarchical JSON in Ruby

查看:64
本文介绍了从Ruby中的分层JSON生成扁平化JSON(反规范化)的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现以下目标.我输入的JSON看起来像这样,

I'm trying to achieve the following. My input JSON looks like this,

{
   "data":{
      "shipping_address":[
         {
            "cust_id":"CUST-123",
            "street":"123 Main St",
            "city":"Atlanta",
            "state":"GA",
            "zip":"12345"
         },
         {
            "cust_id":"CUST-456",
            "street":"456 Front St",
            "city":"Philadelphia",
            "state":"PA",
            "zip":"23456"
         }
      ],
      "orders":[
         {
            "cust_id":"CUST-456",
            "items":[
               {
                  "quantity":"2",
                  "item_code":"ABC-111-222",
                  "cust_id":"CUST-456"
               },
               {
                  "quantity":"1",
                  "item_code":"DEF-999-01-001",
                  "cust_id":"CUST-456"
               }
            ]
         },
         {
            "cust_id":"CUST-123",
            "items":[
               {
                  "quantity":"10",
                  "item_code":"998-111-222",
                  "cust_id":"CUST-123"
               }
            ]
         }
      ],
      "payment":[
         {
            "cust_id":"CUST-123",
            "type":"VISA",
            "card_no":"1234-1111-2222-3333",
            "expiry":"06/2016",
            "billing_add_same_as_shipping":"Y",
            "first_name":"John",
            "last_name":"Smith"
         },
         {
            "cust_id":"CUST-456",
            "type":"VISA",
            "card_no":"5678-4444-8877-5544",
            "expiry":"08/2016",
            "billing_add_same_as_shipping":"N",
            "first_name":"Steve",
            "last_name":"Jones"
         }
      ],
      "billing_address":[
         {
            "cust_id":"CUST-456",
            "street":"7788 Back St",
            "city":"Gainesville",
            "state":"FL",
            "zip":"33444"
         }
      ]
   }
}

我想将此json拼合为两个单独的json

I would like to flatten out this json into two separate jsons

{
   "data":{
      "shipping_address":{
         "cust_id":"CUST-456",
         "street":"456 Front St",
         "city":"Philadelphia",
         "state":"PA",
         "zip":"23456"
      },
      "orders":{
         "cust_id":"CUST-456",
         "items":[
            {
               "quantity":"2",
               "item_code":"ABC-111-222",
               "cust_id":"CUST-456"
            },
            {
               "quantity":"1",
               "item_code":"DEF-999-01-001",
               "cust_id":"CUST-456"
            }
         ]
      },
      "payment":{
         "cust_id":"CUST-456",
         "type":"VISA",
         "card_no":"5678-4444-8877-5544",
         "expiry":"08/2016",
         "billing_add_same_as_shipping":"N",
         "first_name":"Steve",
         "last_name":"Jones"
      },
      "billing_address":{
         "cust_id":"CUST-456",
         "street":"7788 Back St",
         "city":"Gainesville",
         "state":"FL",
         "zip":"33444"
      }
   }
}

{
   "data":{
      "shipping_address":{
         "cust_id":"CUST-123",
         "street":"123 Main St",
         "city":"Atlanta",
         "state":"GA",
         "zip":"12345"
      },
      "orders":{
         "cust_id":"CUST-123",
         "items":[
            {
               "quantity":"10",
               "item_code":"998-111-222",
               "cust_id":"CUST-123"
            }
         ]
      },
      "payment":{
         "cust_id":"CUST-123",
         "type":"VISA",
         "card_no":"1234-1111-2222-3333",
         "expiry":"06/2016",
         "billing_add_same_as_shipping":"Y",
         "first_name":"John",
         "last_name":"Smith"
      }
   }
}

Ruby是否有一种简单的方法来执行此操作而无需循环/解析输入json的每个片段(即通过执行任何JSON映射)?

Is there an easy way in Ruby to perform this without any looping/parsing each fragment of the input json (i.e. by doing any JSON Mapping)?

推荐答案

这是我当前的解决方案

注意:我的输入是来自Java的POJO,其中EvaluationTarget包含输入JSON

Note: my input is a POJO from Java where evaluationTarget contains the input JSON

def json_flattening(input)
    customer_order_hash = Hash.new
    orders_data = input.evaluationTarget
    orders_data.keys.each do |orders_keys|
        orders_data[orders_keys].each do |orders_keys_data|
            if !customer_order_hash.has_key?(orders_keys_data['cust_id']) then
                each_order_hash = Hash.new
                customer_order_hash[orders_keys_data['cust_id']] = each_order_hash
            end

            customer_order_hash[orders_keys_data['cust_id']][orders_keys] = orders_keys_data            
        end
    end
    customer_order_hash.keys.each do |cust_id|
        puts customer_order_hash[cust_id]
    end
end

{"shipping_address"=>{"cust_id"=>"CUST-123", "street"=>"123 Main St", "city"=>"Atlanta", "state"=>"GA", "zip"=>"12345"}, "orders"=>{"cust_id"=>"CUST-123", "items"=>#<Java::NetSfJson::JSONArray:0x69522a14>}, "payment"=>{"cust_id"=>"CUST-123", "type"=>"VISA", "card_no"=>"1234-1111-2222-3333", "expiry"=>"06/2016", "billing_add_same_as_shipping"=>"Y", "first_name"=>"John", "last_name"=>"Smith"}}
{"shipping_address"=>{"cust_id"=>"CUST-456", "street"=>"456 Front St", "city"=>"Philadelphia", "state"=>"PA", "zip"=>"23456"}, "orders"=>{"cust_id"=>"CUST-456", "items"=>#<Java::NetSfJson::JSONArray:0x40030597>}, "payment"=>{"cust_id"=>"CUST-456", "type"=>"VISA", "card_no"=>"5678-4444-8877-5544", "expiry"=>"08/2016", "billing_add_same_as_shipping"=>"N", "first_name"=>"Steve", "last_name"=>"Jones"}, "billing_address"=>{"cust_id"=>"CUST-456", "street"=>"7788 Back St", "city"=>"Gainesville", "state"=>"FL", "zip"=>"33444"}}

这篇关于从Ruby中的分层JSON生成扁平化JSON(反规范化)的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆