5

On my host I enter the following, which returns a bunch of information which isn't particularly easy to read at the CLI.

echo $ENV_VAR | base64 --decode

Is there a way to format it?

This is a sample output from the command.

{"something": [{"path": "something", "host": "something.internal", "scheme": "solr", "port": 8080, "ip": "123.4.567.89"}], "second_database": [{"username": "user", "password": "", "ip": "123.4.567.89", "host": "second_database.internal", "query": {"is_master": true}, "path": "main", "scheme": "mysql", "port": 3306}], "redis": [{"ip": "123.4.567.89", "host": "redis", "scheme": "redis", "port": 6379}], "database": [{"username": "user", "password": "", "ip": "123.4.567.89", "host": "database.internal", "query": {"is_master": true}, "path": "main", "scheme": "mysql", "port": 3306}]}

It's probably worth pointing out that my host, like many, offers a read-only file system.

  • There are thousands of ways to format some text. At least give the input (i.e. the output from base64 --decode and the desired format, including preferred tools used for formatting that (so I don't come up with some Haskell based solution where you only can fine tune things in Occam. – Anthon Jun 18 '15 at 9:46
  • Yes, but depends entirely on what the decoded content looks like. – Sobrique Jun 18 '15 at 10:03
  • @Pandya I have updated my question with the sample output from the command. – crmpicco Jun 18 '15 at 12:37
  • @Anthon Please see update. – crmpicco Jun 18 '15 at 12:37
  • @Sobrique Question has been updated. – crmpicco Jun 18 '15 at 12:37
12
cat file.json | json_pp  #perl utility
cat file.json | jq .    

jq packs much more than just pretty-printing abilities.

  • 1
    (I know that I abuse cats and I don't care.) – PSkocik Jun 18 '15 at 13:19
  • Sadly I don't have a file (read-only file system). What I have to work from is the output from the echo command I gave in my question. – crmpicco Jun 18 '15 at 13:27
  • 2
    echo "$your_json" | ... – PSkocik Jun 18 '15 at 13:30
  • 2
    You don't need cat for jq, jq . file.json works just as well. – k0pernikus Nov 23 '15 at 17:48
  • 1
    @k0pernikus Yeah. or < file.json theCommand. The performance overhead of cat probably doesn't matter here, and the cat version has some syntactic advantages. Everything's a tradeoff. Taste plays a role too. – PSkocik Nov 23 '15 at 17:59
2

I would pipe that into yaml (which is part of ruamel.yaml of which I am the author):

echo $ENV_VAR | base64 --decode | yaml from-json -

will give you this (based on your example output):

second_database:
- username: user
  password: ''
  ip: 123.4.567.89
  host: second_database.internal
  path: main
  query:
    is_master: true
  scheme: mysql
  port: 3306
redis:
- ip: 123.4.567.89
  host: redis
  scheme: redis
  port: 6379
something:
- path: something
  host: something.internal
  scheme: solr
  port: 8080
  ip: 123.4.567.89
database:
- username: user
  password: ''
  ip: 123.4.567.89
  host: database.internal
  path: main
  query:
    is_master: true
  scheme: mysql
  port: 3306

The ordering of the keys is not guaranteed, because it is not guaranteed in json and I don't have a ruamel.json package like ruamel.yaml that preserves order when reading in json.

The above works on the principle that YAML is a superset of JSON, but has more readable display modes (leaving out quotes when not absolutely necessary, making indented block structure).

  • I thought that you could pipe directly into yaml I have to check why that is not working. Until then you would need to write a temporary file. yaml writes to stdout – Anthon Jun 18 '15 at 12:53
  • Thanks, the format that you have given here is exactly what i'm looking for. Unfortunately I need a CLI tool that doesn't require writing to the file system as my host offers only a read-only file system...and yaml is not installed. – crmpicco Jun 18 '15 at 12:57
  • I fixed the "piping" in ruamel.yaml-0.9.8, but you would still need to install that on the system ( pip install ruamel.yaml) – Anthon Jun 18 '15 at 13:33
1

OK, that output is JSON. So parse as JSON:

#!/usr/bin/perl;
use strict;
use warnings;

use JSON;

local $/; 
print to_json ( decode_json ( <> ), {pretty => 1 });

Will print your thing as:

{
   "something" : [
      {
         "host" : "something.internal",
         "scheme" : "solr",
         "ip" : "123.4.567.89",
         "path" : "something",
         "port" : 8080
      }
   ],
   "second_database" : [
      {
         "path" : "main",
         "ip" : "123.4.567.89",
         "query" : {
            "is_master" : true
         },
         "host" : "second_database.internal",
         "port" : 3306,
         "password" : "",
         "scheme" : "mysql",
         "username" : "user"
      }
   ],
   "redis" : [
      {
         "scheme" : "redis",
         "ip" : "123.4.567.89",
         "port" : 6379,
         "host" : "redis"
      }
   ],
   "database" : [
      {
         "username" : "user",
         "password" : "",
         "scheme" : "mysql",
         "port" : 3306,
         "host" : "database.internal",
         "ip" : "123.4.567.89",
         "query" : {
            "is_master" : true
         },
         "path" : "main"
      }
   ]
}

Formatted, and for bonus points - still JSON.

1

Try echo $ENV_VAR | base64 --decode | json_reformat. If this is a 6.x or higher Centos version, you should have it

0

The following npmjs package does the task: https://www.npmjs.com/package/js-beautify

You can install it with

npm -g install js-beautify

globally, although overwriting the debian packages is baaaad practice. Better if you create your own global or local package set and use the tool from that.

You need nodejs for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.