Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 559
Chris Connett Avatar asked Mar 08 '16 00:03

Chris Connett


People also ask

How do I format a JSON file?

You can format your JSON document using Ctrl+Shift+I or Format Document from the context menu.

How do you format a clang file?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

Can clang-format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.


1 Answers

Another program that I like to use is jq. It's pretty easy to use, and the documentation is great. For example, for simple reformatting you can do this:

jq . test.json
like image 62
PythonJin Avatar answered Sep 28 '22 11:09

PythonJin