rsl

reserialise: lossy but versatile conversion between data serialisation formats


installation

    $ go install go.senan.xyz/[email protected]

usage

command line
    $ rsl <src format> <dest format>
available formats
  • csv
  • js (javascript objects, decode only)
  • json
  • toml
  • xml (lossy support for arbitrary objects)
  • xmlstd (lossless but limited)
  • yaml

examples

    $ rsl toml csv <some-toml.toml >some-csv.csv

    $ cat example.json
    [
        {
          "name": "jim",
          "addr": "dublin"
        },
        {
          "name": "miguel",
          "addr": "space"
        }
    ]

    $ rsl json json <example.json
    [{"addr":"dublin","name":"jim"},{"addr":"space","name":"miguel"}]

    $ rsl json toml <example.json
    [[result]]
      addr = "dublin"
      name = "jim"

    [[result]]
      addr = "space"
      name = "miguel"

    $ rsl json yaml <example.json
    - addr: dublin
      name: jim
    - addr: space
      name: miguel

    $ rsl json csv <example.json
    name,addr
    jim,dublin
    miguel,space

    $ cat example.simple.json
    [
        [
          "jim",
          "dublin"
        ],
        [
          "miguel",
          "space"
        ]
    ]

    # generate pseudo headers if there are none. makes it easy to query with other tools
    $ rsl json csv <example.simple.json
    a,b
    jim,dublin
    miguel,space

    $ cat example.html
    <select id="cars">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>

    $ rsl xml json <example.html | jq
    {
      "select": {
        "@id": "cars",
        "option": [
          {
            "#text": "Volvo",
            "@value": "volvo"
          },
          {
            "#text": "Saab",
            "@value": "saab"
          },
          {
            "#text": "Opel",
            "@value": "opel"
          },
          {
            "#text": "Audi",
            "@value": "audi"
          }
        ]
      }
    }

    $ rsl xml json <example.html | jq -r '.select.option | .[] | select(."@value" == "saab") | ."#text"'
    Saab

    $ cat example.csv
    year,artist,album
    1981,Alan Vega,"Collision Drive"
    1991,Underground Resistance,"The Final Frontier"
    1999,Dopplereffekt,Gesamtkunstwerk
    2002,Dopplereffekt,Myon-Neutrino
    2016,Pangaea,"In Drum Play"

    $ rsl csv json <example.csv | jq -r '.[] | select(.artist == "Dopplereffekt") | .album'
    Gesamtkunstwerk
    Myon-Neutrino

    # parse javascript objects
    $ node -e 'console.log({colour: "blue", v: Math.random()})' | rsl js yaml
    colour: blue
    v: 0.7409782831156317

GitHub

View Github