0

I've tried using the csvtojson module to create a GeoJSON-formatted file, but the nesting isn't working correctly at all. Can anyone point me in the right direction or do I need to write my own code?

> npx csvtojson input.tsv > output.json

input.tsv

properties.labelTc  properties.labelEn  properties.nameTc   properties.nameEn   properties.zoomifyX properties.zoomifyY geometry.coordinates.1  geometry.coordinates.0  properties.urlEn    properties.urlZh    type
皇城  The Imperial Palace City    明故宫 Ming Palace 105513  -1863   32.038  118.815 https://en.wikipedia.org/wiki/Ming_Palace       Feature
天地壇 Altar of Heaven and Earth       Guanghuamen?    105049  -1000   32.058  118.832     https://baike.baidu.com/item/%E5%A4%A9%E5%9D%9B/19964669    Feature

What I want

{
  "properties": {
    "labelTc": "皇城",
    "labelEn": "The Imperial Palace City",
    ...
  },
  "geometry": {
    "coordinates": [118.815, 32.038]
  },
  "type": "Feature"
}

What I got:

[
  {
    "properties": {
      "labelTc\tproperties": {
        "labelEn\tproperties": {
          "nameTc\tproperties": {
            "nameEn\tproperties": {
              "zoomifyX\tproperties": {
                "zoomifyY\tgeometry": {
                  "coordinates": {
                    "1\tgeometry": {
                      "coordinates": {
                        "0\tproperties": {
                          "urlEn\tproperties": {
                            "urlZh\ttype": "??\tThe Imperial Palace City\t???\tMing Palace\t105513\t-1863\t32.038\t118.815\thttps://en.wikipedia.org/wiki/Ming_Palace\t\tFeature"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
carpiediem
  • 1,918
  • 22
  • 41
  • Perhaps I just need to se the right flags? This tool (linked from the repo) is working... https://csv.keyangxiang.com/ – carpiediem May 11 '20 at 11:09
  • I fixed the nesting issue with the `delimiter` flag: `npx csvtojson --delimiter=\t input.tsv > output.json` – carpiediem May 14 '20 at 07:38
  • Still having problems with Chinese character encoding though. I added an issue to the repo, since that would seem to be the root cause. https://github.com/Keyang/node-csvtojson/issues/388 – carpiediem May 14 '20 at 07:39
  • 1
    You have a tsv (tab separated values) not a csv (comma s v) – Bohemian May 14 '20 at 07:47
  • Sorry, that was just a typo. I had tried both and copied different bits into this question. I'll fix that now. – carpiediem May 14 '20 at 10:35

1 Answers1

0

Since I was using Windows PowerShell, which uses an extension of Latin-1 encoding, by default, I needed both a flag for the csvtojson library and a second for PowerShell.

npx csvtojson --delimiter=\t input.tsv > output.json -encoding utf8
carpiediem
  • 1,918
  • 22
  • 41