How to format json string to pretty printed in Kate

If I paste a string of Json data into the Kate text editor, I would like to alter the string so that this:

{"fruit": "Apple","size": "Large","color": "Red"}

becomes this:

{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}
2 Likes

I’m not sure if Kate supports something as this natively, but you could use command line utility jq:

$ echo '{"fruit": "Apple","size": "Large","color": "Red"}' | jq
{
  "fruit": "Apple",
  "size": "Large",
  "color": "Red"
}

Just note, you have to install it as it is not preinstalled:

sudo dnf install jq
4 Likes

See also:

$ json_reformat << EOF
{"fruit": "Apple","size": "Large","color": "Red"}
EOF
{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}

$ rpm -q -f $(type -p json_reformat)
yajl-2.1.0-12.fc30.x86_64
4 Likes

I’ve not seen too many editors that will break lines for you like that. Even with Vim, it breaks lines if you specify the maximum line-width etc. However, in most editors, if you specify the different bits in different lines, they will do the indenting for you.

Tools -> Filter Text -> and then enter json_reformat, untick ‘Copy the result instead of pasting it’,
hit Enter seems to do the job

3 Likes

I have seen it in Notepad++

Never used that, so I wouldn’t know.

For completeness, there seem to be multiple plugins/tweaks that get VIM to do it. This is one, for example: https://blog.trueheart78.com/2018/01/22/make-vim-format-json-for-you.html or https://stackoverflow.com/questions/26214156/how-to-auto-format-json-on-save-in-vim

2 Likes

If this is the solution, please mark it as such.

1 Like