JSON到CSV架构

时间:2011-06-23 14:48:08

标签: json csv schema

我有一些银行帐户信息的JSON数据结构,我将其导出为CSV文件,以便在Microsoft Excel中打开。每个帐户的JSON是:

{
  "apy": 2.0,
  "product_type": "Investors Checking",
  "features":  {
    "ATM_FEES": "Refunded",
    "ATM_CARD_AVAILABLE": "Yes",
    "SIMPLY_MAINTAIN_A_MONTHLY_BALANCE_OF": "$10,000"
  },
  "min_investment": "",
  "max_investment": 20000,
  "institution_type": "Credit Union",
  "institution_num": 11307,
  "institution": "Apple Federal Credit Union"
}

除了“features”字典之外,我可以使用列导出它。最终会成为包含对象的列:

{
  "ATM_FEES": "Refunded",
  "ATM_CARD_AVAILABLE": "Yes",
  "SIMPLY_MAINTAIN_A_MONTHLY_BALANCE_OF": "$10,000"
}

对于任何给定的银行,功能字典可以是具有各种功能的任意长度。我主要拥有面向文档的数据库(MongoDB)的经验。

我应该如何为相同的数据构建关系模式?

2 个答案:

答案 0 :(得分:3)

此处CSV和关系结构不匹配。 CSV可以具有任意数量的字段,每个要素都作为单独的列。在关系数据库中,您可以采用不同的方式。我会建议一个基本数据表,一个用于功能。像这样:

表BANK_ACCOUNT_INFO:

  • ID
  • APY
  • PRODUCT_TYPE
  • min_investment
  • max_investment
  • institution_type
  • institution_num
  • 机构

表BANK_ACCOUNT_FEATURES:

  • ID
  • BANK_ACCOUNT_ID
  • FEATURE_NAME
  • FEATURE_VALUE

基本表中的1条记录可以与要素表中的多条记录相关联。

答案 1 :(得分:0)

以下是使用jq

的解决方案
def headers:
    keys_unsorted[] as $k
  | if   .[$k]|type == "object" then (.[$k]|headers)
    else $k
    end
;

def data:
    keys_unsorted[] as $k
  | if   .[$k]|type == "object" then (.[$k]|data)
    else .[$k]
    end
;

  (.[0] | [headers])
, (.[]  | [data])
| @csv

如果filter.jq包含此过滤条件且data.json包含样本数据,则

$ jq -Mrs -f filter.jq data.json

将产生

"apy","product_type","ATM_FEES","ATM_CARD_AVAILABLE","SIMPLY_MAINTAIN_A_MONTHLY_BALANCE_OF","min_investment","max_investment","institution_type","institution_num","institution"
2,"Investors Checking","Refunded","Yes","$10,000","",20000,"Credit Union",11307,"Apple Federal Credit Union"