Chris Connett Chris Connett - 7 months ago 35
Javascript Question

clang-format a json file

I have a json file. If I run clang-format on it, it formats it as though it's code (ugly).

{
"name" : "My great app",
"description" : "It's really cool.",
"version" : "0.0.1"
}


If I put 'foo = ' at the start of the file, it's great, but it's not json anymore.

foo = {
"name" : "My great app",
"description" : "It's really cool.",
"version" : "0.0.1"
}


How can I get clang-format to format the bare object in the json file as in the second example?

zmo zmo
Answer

Personally I'd do it using python, using the json's package pretty printer:

cat mydata.json | python -mjson.tool

and if you don't like the defaults:

cat mydata.json | python -c 'import json, sys; print(json.dumps(json.load(sys.stdin), indent=4, sort_keys=True))'

Otherwise, I don't have clang-format installed, and for the sake of pretty printing, I'd rather use an existing tool.

N.B.: You can also do it within vim and use the == normal command on the full file selection ☺

Comments