如何格式化使用JSON数据的csv文件?(How to format a csv file usin

2019-09-28 02:31发布

我有我需要转换为csv文件JSON文件,但我有点担心的输出数据似乎是不正确信任的JSON到CSV格式转换器网站的......所以我希望得到一些帮助这里!

我有以下的JSON文件结构:

{
    "GroupName": "GrpName13",
    "Number": 3,
    "Notes": "Test Group ",
    "Units": [
    {
        "UnitNumber": "TestUnit13",
        "DataSource": "Factory",
        "ContractNumber": "TestContract13",
        "CarNumber": "2",
        "ControllerTypeMessageId" : 4,
        "NumberOfLandings": 4,
        "CreatedBy": "user1",
        "CommissionModeMessageId": 2,
        "Details": [
        {
            "DetailName": "TestFloor13",
            "DetailNumber": "5"
        }
        ],
        "UnitDevices": [
        {
            "DeviceTypeMessageId": 1,
            "CreatedBy": "user1"
        }                               
        ]
    }
    ]
}

我觉得我看到的问题是,该转换器似乎无法领悟许多嵌套数据值。 我认为转换器是错误的原因是因为当我尝试使用回它们转换为JSON,我不接受相同的结构。

有谁知道如何手动格式化这个JSON为csv格式,或知道一个可靠的转换器的比可以处理嵌套的价值?

Answer 1:

尝试

www.json-buddy.com/convert-json-csv-xml.htm

如果不是为你工作,那么你可以试试这个工具

http://download.cnet.com/JSON-to-CSV/3000-2383_4-76680683.html

应该是有帮助!



Answer 2:

我曾尝试你的JSON在这个网址为:

http://www.convertcsv.com/json-to-csv.htm

其结果是:UnitNumber,数据源,ContractNumber,CarNumber,ControllerTypeMessageId,NumberOfLandings,CreatedBy,CommissionModeMessageId,细节/ 0 / DetailName,细节/ 0 / DetailNumber,UnitDevices / 0 / DeviceTypeMessageId,UnitDevices / 0 / CreatedBy TestUnit13,工厂,TestContract13,2 ,4,4,user1,2,TestFloor13,5,1,USER1

因为它可以保存密钥的路径,比如在列表“UnitDevices”的“DeviceTypeMessageId”:它会指定的列有“UnitDevices / 0 / DeviceTypeMessageId”的名字,这可避免同名的错误,这样你就可以得到列它的转换规则名称。

希望有帮助。



Answer 3:

这里是一个解决方案使用JQ

如果文件filter.jq包含

def denormalize:
  def headers($p):
      keys_unsorted[] as $k
    | if   .[$k]|type == "array" then (.[$k]|first|headers("\($p)\($k)_"))
      else "\($p)\($k)"
      end
  ;
  def setup:
    [
      keys_unsorted[] as $k
    | if   .[$k]|type == "array" then [ .[$k][]| setup ]
      else .[$k]
      end
    ]
  ;
  def iter:
    if length == 0 then []
    elif .[0]|type != "array" then
      [.[0]] + (.[1:] | iter)
    else
        (.[0][] | iter) as $x
      | (.[1:]  | iter) as $y
      | [$x[]] + $y
    end
  ;
  [ headers("") ], (setup | iter)
;

denormalize | @csv

data.json包含(注意额外补充样品)

{
  "GroupName": "GrpName13",
  "Notes": "Test Group ",
  "Number": 3,
  "Units": [
    {
      "CarNumber": "2",
      "CommissionModeMessageId": 2,
      "ContractNumber": "TestContract13",
      "ControllerTypeMessageId": 4,
      "CreatedBy": "user1",
      "DataSource": "Factory",
      "Details": [
        {
          "DetailName": "TestFloor13",
          "DetailNumber": "5"
        }
      ],
      "NumberOfLandings": 4,
      "UnitDevices": [
        {
          "CreatedBy": "user1",
          "DeviceTypeMessageId": 1
        },
        {
          "CreatedBy": "user10",
          "DeviceTypeMessageId": 10
        }
      ],
      "UnitNumber": "TestUnit13"
    },
    {
      "CarNumber": "99",
      "CommissionModeMessageId": 99,
      "ContractNumber": "Contract99",
      "ControllerTypeMessageId": 99,
      "CreatedBy": "user99",
      "DataSource": "Another Factory",
      "Details": [
        {
          "DetailName": "TestFloor99",
          "DetailNumber": "99"
        }
      ],
      "NumberOfLandings": 99,
      "UnitDevices": [
        {
          "CreatedBy": "user99",
          "DeviceTypeMessageId": 99
        }
      ],
      "UnitNumber": "Unit99"
    }
  ]
}

然后命令

jq -M -r -f filter.jq data.json    

会产生

"GroupName","Notes","Number","Units_CarNumber","Units_CommissionModeMessageId","Units_ContractNumber","Units_ControllerTypeMessageId","Units_CreatedBy","Units_DataSource","Units_Details_DetailName","Units_Details_DetailNumber","Units_NumberOfLandings","Units_UnitDevices_CreatedBy","Units_UnitDevices_DeviceTypeMessageId","Units_UnitNumber"
"GrpName13","Test Group ",3,"2",2,"TestContract13",4,"user1","Factory","TestFloor13","5",4,"user1",1,"TestUnit13"
"GrpName13","Test Group ",3,"2",2,"TestContract13",4,"user1","Factory","TestFloor13","5",4,"user10",10,"TestUnit13"
"GrpName13","Test Group ",3,"99",99,"Contract99",99,"user99","Another Factory","TestFloor99","99",99,"user99",99,"Unit99"


文章来源: How to format a csv file using json data?