You don't need web sites to base64 decode things. Or to format JSON

Instead of using free sites to do encoding / decoding / formatting of data, you could be using the command line. Here's an example of base64 decoding and pretty formatting json.

You don't need web sites to base64 decode things. Or to format JSON

pipes are awesome

I’m currently working with Mixpanel.

Integrations send data to its API in base64 encoded format. The queries are logged, but they're the base64 encoded ones, so if I want to see what it actually is, then I need to base64 decode that string.

Previously I would search for “base 64 decoder” on them Googles, and hurl the string into a text box, and get a decoded JSON in a different text box. But since I don’t trust websites (what if they log the queries?), and this was dealing with privileged data, I wanted to find a way to do this manually.

Enter command line. OSX has a tool called base64, which you can use to encode/decode strings.

$ echo VGhpcyBpcyBhIHN0cmluZwo= | base64 --decode
This is a string

JSON data in files / assembled will probably have all its whitespace removed, so doing a cat would result in something like this:

$ cat something.json
[{"property":"value","another_property":"escaped\\value"},{"name":"thing","number":"bla"}]

Not bad, readable, but inconvenient. Mixpanel would send JSON data like the above (but with properties the API actually needs) base64 encoded, so viewing that would mean:

$ echo W3sicHJvcGVydHkiOiJ2YWx1ZSIsImFub3RoZXJfcHJvcGVydHkiOiJlc2NhcGVkXFx2YWx1ZSJ9LHsibmFtZSI6InRoaW5nIiwibnVtYmVyIjoiYmxhIn1dCg== | base64 --decode
[{"property":"value","another_property":"escaped\\value"},{"name":"thing","number":"bla"}]

Enter jq

jq is a utility you should install with homebrew:

$ brew install jq

After that you can do cool stuff:

$ cat something.json | jq
[
  {
    "property": "value",
    "another_property": "escaped\\value"
  },
  {
    "name": "thing",
    "number": "bla"
  }
]

And tying everything together:

$ echo W3sicHJvcGVydHkiOiJ2YWx1ZSIsImFub3RoZXJfcHJvcGVydHkiOiJlc2NhcGVkXFx2YWx1ZSJ9LHsibmFtZSI6InRoaW5nIiwibnVtYmVyIjoiYmxhIn1dCg== | base64 --decode | jq
[
  {
    "property": "value",
    "another_property": "escaped\\value"
  },
  {
    "name": "thing",
    "number": "bla"
  }
]

Summary

  1. get a base64 encoded json
  2. use the built in base64 decoder to turn that into json
  3. get jq and use that to pretty-format said json
$ echo <base64 json> | base64 --decode | jq

Photo by Rodion Kutsaev on Unsplash