TomWright / dasel

Select, put and delete data from JSON, TOML, YAML, XML and CSV files with a single tool. Supports conversion between formats and can be used as a Go package.
https://daseldocs.tomwright.me
MIT License
7.11k stars 135 forks source link

CSV writer can't output a list of objects #110

Closed alexkreidler closed 3 years ago

alexkreidler commented 3 years ago

I have this json:

[
  {
    "id": "ABS",
    "name": "Australian Bureau of Statistics"
  },
  {
    "id": "ECB",
    "name": "European Central Bank"
  },
  {
    "id": "ESTAT",
    "name": "Eurostat"
  },
  {
    "id": "ILO",
    "name": "International Labor Organization"
  }
]

Then I run the following and get:

$ cat test.json | dasel -r json -w csv                                                                                                                                                                (base) 
Error: could not write output: could not write to output file: could not get byte data for file: CSVParser.toBytes cannot handle type []interface {}
Usage:
  dasel select -f <file> -p <json,yaml> -s <selector> [flags]

Flags:
  -c, --compact           Compact the output by removing all pretty-printing where possible.
  -f, --file string       The file to query.
  -h, --help              help for select
      --length            Output the length of the selected value.
  -m, --multiple          Select multiple results.
  -n, --null              Output null instead of value not found errors.
  -p, --parser string     Shorthand for -r FORMAT -w FORMAT.
      --plain             Alias of -w plain
  -r, --read string       The parser to use when reading.
  -s, --selector string   The selector to use when querying the data structure.
  -w, --write string      The parser to use when writing.

Error: could not write output: could not write to output file: could not get byte data for file: CSVParser.toBytes cannot handle type []interface {}

I then tried some slight modifications. Running cat test.json | dasel -p json -w csv ".[*]" gets: Error: could not query node: could not find value: selector is not supported here: .[*]

Then, running cat test.json | dasel -p json -w csv ".[*]" -m outputs the following:

map[id:ABS name:Australian Bureau of Statistics]
map[id:ECB name:European Central Bank]
map[id:ESTAT name:Eurostat]
map[id:ILO name:International Labor Organization]

which is better, but it should be comma separated values.

Please let me know if I can provide any more information. Thanks for this great project!

TomWright commented 3 years ago

Hey @alexkreidler,

Thanks for raising this.

I've fixed the issue and it will be released under v1.13.6 shortly.

echo '[
  {
    "id": "ABS",
    "name": "Australian Bureau of Statistics"
  },
  {
    "id": "ECB",
    "name": "European Central Bank"
  },
  {
    "id": "ESTAT",
    "name": "Eurostat"
  },
  {
    "id": "ILO",
    "name": "International Labor Organization"
  }
]' | dasel -r json -w csv
id,name
ABS,Australian Bureau of Statistics
ECB,European Central Bank
ESTAT,Eurostat
ILO,International Labor Organization