5

I'm working with avro .avsc schemas now and I like formatting in samples:

{
 "namespace": "example.avro",
 "type": "record",
 "name": "User",
 "fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number",  "type": ["int", "null"]},
     {"name": "favorite_color", "type": ["string", "null"]}
 ]
}

I mean one-line field definitions {"name": "name", "type": "string"} specifically.

I'd like to edit and format my schemas in vscode, but it keeps wrapping fields like

 {
   "name": "name",
   "type": "string"
 }

I tried to configure default json formatter and with beautify extension in setting.json, but got no luck

   "[json]": {
        "editor.tabSize": 2,
        "editor.wordWrap": "off",
        "editor.defaultFormatter": "vscode.json-language-features",
        // "editor.defaultFormatter": "HookyQR.beautify"
    },
    "html.format.wrapAttributes": "preserve",

    "beautify.config": {
        "indent_size": 2,
        "indent_char": " ",
        "preserve_newlines": true,
        "space_in_paren": true,
        "space_in_empty_paren": true,
        "wrap_attributes": "preserve", 
    },
  • 1
    See https://stackoverflow.com/a/73180702/836330 - there is a new setting that will keep the info on the same line as you wanted. – Mark Jul 31 '22 at 04:44

1 Answers1

3

The FracturedJson VSCode extension might be useful for you. (I'm the author. It's open source under the MIT license.)

The basic idea is that objects and arrays are written on single lines, as long as they're not too long and not too deeply nested.

There's a browser version if you'd like to play around with the capabilities before downloading the extension.

j-brooke
  • 176
  • 1
  • 4
  • Ah, shoot. I was hopeful that this one might be "the one for me", but it looks like it doesn't handle comments. : ( – MostHated Nov 26 '21 at 20:15
  • It took some fiddling with the settings, but this helped me to format and bulk 6 million lines of JSON into ElasticSearch with ease. Thanks a lot! – James Hooper May 11 '22 at 17:21