0

I need help about how to convert long line of json file to readable file

this is the json format

"version": 1, "partitions": [{"topic": "Topic3", "partition": 7, "log_dirs": ["any"], "replicas": [3, 0, 2]}, {"topic": "Topic3", "partition": 4, "log_dirs": ["any"], "replicas": [3, 0, 2]}, {"topic": "Topic3", "partition": 15, "log_dirs": ["any"], "replicas": [2, 3, 0]}, {"topic": "Topic3", "partition": 9, "log_dirs": ["any"], "replicas": [2, 3, 0]}, {"topic": "Topic3", "partition": 12, "log_dirs": ["any"], "replicas": [2, 3, 0]}, {"topic": "Topic3", "partition": 1, "log_dirs": ["any"], "replicas": [3, 2, 0]}, {"topic": "CatchAllTopic", "partition": 0, "log_dirs": ["any"], "replicas": [0, 3, 2]}, {"topic": "Topic3", "partition": 17, "log_dirs": ["any"], "replicas": [0, 3, 2]}, {"topic": "Topic3", "partition": 6, "log_dirs": ["any"], "replicas": [2, 0, 3]}, {"topic": "Topic3", "partition": 3, "log_dirs": ["any"], "replicas": [2, 0, 3]}, {"topic": "Topic3", "partition": 14, "log_dirs": ["any"], "replicas": [0, 2, 3]}, {"topic": "Topic3", "partition": 0, "log_dirs": ["any"], "replicas": [2, 0, 3]}, {"topic": "Topic3", "partition": 11, "log_dirs": ["any"], "replicas": [0, 2, 3]}, {"topic": "Topic3", "partition": 16, "log_dirs": ["any"], "replicas": [3, 0, 2]}, {"topic": "Topic3", "partition": 8, "log_dirs": ["any"], "replicas": [0, 3, 2]}, {"topic": "Topic3", "partition": 2, "log_dirs": ["any"], "replicas": [0, 3, 2]}, {"topic": "Topic3", "partition": 13, "log_dirs": ["any"], "replicas": [3, 0, 2]}, {"topic": "Topic3", "partition": 5, "log_dirs": ["any"], "replicas": [0, 3, 2]}, {"topic": "Topic3", "partition": 10, "log_dirs": ["any"], "replicas": [3, 0, 2]}]}

or actually more like this:

enter image description here

while we want the file as the following format

more file.json

{"partitions":
       [{"topic": "foo",
         "partition": 1,
          "replicas": [1,2,3],
          "log_dirs": ["any","any","any"]
       }],
.
.
.
.
.
Judy
  • 1,595
  • 6
  • 19
  • 41
  • 3
    Maybe `jq . file.json`? – dibery Dec 23 '19 at 16:22
  • Unless you can provide more context, @dibery's suggestion sounds about right. – sshine Dec 23 '19 at 16:24
  • see the update in my question – Judy Dec 23 '19 at 16:28
  • so after we covert it to readable , then how to convert it back as previews ? – Judy Dec 23 '19 at 16:38
  • 2
    @Judy Use the `--compact-output` option to `jq` to make it print output on one line. – Barmar Dec 23 '19 at 16:48
  • Does this answer your question? [How can I pretty-print JSON in a shell script?](https://stackoverflow.com/questions/352098/how-can-i-pretty-print-json-in-a-shell-script) – Léa Gris Dec 23 '19 at 17:04
  • 1
    Neither of the sample inputs is valid JSON, and since both include a trailing `}`, it would appear the initial `{` is also missing. Please clarify -- is the question in essence about valid JSON or about fixing up mangled data to make it valid JSON, or something else? – peak Dec 23 '19 at 17:13

1 Answers1

1

You can pretty print a json string using Python 3 if you cant install jq for some reason :

echo '{ "name" : "Matias", "age": 32 }' | python3 -c 'import sys
import json
print(json.dumps(json.load(sys.stdin), indent="  "))
'

This will print :

{
  "name": "Matias",
  "age": 32
}

Hope it's useful.

Matias Barrios
  • 4,674
  • 3
  • 22
  • 49