将CSV转换为JSON解决方案

时间:2014-11-03 12:11:41

标签: json csv parse-platform

我刚刚为后端服务器使用parse开发了一个iOS应用程序。我有完整的应用程序,准备好了,并有大量的entires添加到解析后端,但是我已经知道数据没有实现,直到现在加载一个包括地理点的类我需要使用json。我的数据文件结构如下:

Country,PostCode,State,Suburb,location/__type,location/latitude,location/longitude,locname,phone,streetaddress,website
Australia,2000,NSW,Cronulla,GeoPoint,-33.935434,151.026887,Shop ABC,+61297901401,ABC Canterbury Road,http://www.123.com.au

我需要将此格式转换为以下内容

{ "results": [
    {
        "Country": "Australia",
        "PostCode": "2000",
        "State": "NSW",
        "Suburb": “Crounlla”,
        "location": {
            "__type": "GeoPoint",
            "latitude": -33.935434,
            "longitude": 151.026887
        },
        "locname": "Shop ABC”,
        "phone": "+123456”,
        "streetaddress": “ABC Canterbury Road",
        "website": "http://www.123.com.au"
    }
] }

我有几千个条目,因此您可以想象我不想手动执行此操作。我只能访问Mac,因此任何建议都需要Mac友好。之前的答案我发现由于地理数据而无效。

3 个答案:

答案 0 :(得分:2)

你可以使用python脚本(Mac预装了python) 示例代码:

#!/usr/bin/python

import csv
import json


header = []
results = []
with open('data.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        if len(header) > 0:
            location = {}
            datarow = {}
            for key, value in (zip(header,row)):
                if key.startswith('location'):
                    location[key.split('/')[1]] = value
                else:
                    datarow[key] = value
            datarow['location'] = location
            results.append(datarow)
        else:
            header = row

        print json.dumps(dict(results=results))

答案 1 :(得分:1)

也许您可以使用http://www.convertcsv.com/csv-to-json.htm -website转换这些内容?

答案 2 :(得分:1)

以下是使用jq的解决方案。

如果filter.jq包含以下过滤器

def parse:
  [
      split("\n")[]                       # split string into lines
    | split(",")                          # split data
    | select(length>0)                    # eliminate blanks
  ]
;

def reformat:
  [
      .[0]    as $h                       # headers
    | .[1:][] as $v                       # values
    | [   [$h, $v]                   
        | transpose[]                     # convert array 
        | {key:.[0], value:.[1]}          # to object
      ] | from_entries                    #
    | reduce (
          keys[]                          # 
        | select(startswith("location/")) # move keys starting
      ) as $k (                           # with "location/"
          .                               # into a "location" object
        ; setpath($k|split("/");.[$k])    # 
        | delpaths([[$k]])                #
      )
    | .location.latitude  |= tonumber     # convert "latitude" and
    | .location.longitude |= tonumber     # "longitude" to numbers
  ]
; 

{
  results: (parse | reformat)
}

data包含示例数据,然后是命令

$ jq -M -Rsr -f filter.jq data

产生

{
  "results": [
    {
      "Country": "Australia",
      "PostCode": "2000",
      "State": "NSW",
      "Suburb": "Cronulla",
      "locname": "Shop ABC",
      "phone": "+61297901401",
      "streetaddress": "ABC Canterbury Road",
      "website": "http://www.123.com.au",
      "location": {
        "__type": "GeoPoint",
        "latitude": -33.935434,
        "longitude": 151.026887
      }
    }
  ]
}