Text filters to format JSON and XML

3,031 views
Skip to first unread message

Arthur Goldberg

unread,
May 26, 2021, 12:42:09 PM5/26/21
to BBEdit Talk
I'm looking for text filters to format JSON and XML. Google and https://www.barebones.com/search.html finds things that are not well documented or seem outdated.

For macOS Big Sur and Python 3 please.

Thanks
Arthur

jj

unread,
May 26, 2021, 3:06:39 PM5/26/21
to BBEdit Talk
Hi Arthur,

For JSON, I am very happy with 'jq'.


Install it from homebrew:

    % brew install jq
    
or download it from their site.

Then create a text filter named "jq.sh" like so:

    ```
    #!/usr/bin/env sh
    
    jq '.'
    
    ```
place the text filter in BBEdit's "Text Filters" folder and you should be good to go.

For XML, I think macOS comes with xmllint installed.
But this is not the case on your setup, you can install it from Homebrew:

    % brew install libxml2
    
Then create a text filter named "xmllint.sh" like so:

    ```
    #!/usr/bin/env sh

    xmllint --format -
    
    ```
place the text filter in BBEdit's "Text Filters" folder and you should be good to go.

HTH

Jean Jourdain

Patrick Woolsey

unread,
May 26, 2021, 3:54:50 PM5/26/21
to bbe...@googlegroups.com
For XML, there's Markup -> Tidy -> Reflow Document... (and its relatives ;-).


Regards

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>

Charlie Garrison

unread,
May 26, 2021, 10:08:56 PM5/26/21
to BBEdit Talk

On 27 May 2021, at 2:41, Arthur Goldberg wrote:

I'm looking for text filters to format JSON and XML. Google and https://www.barebones.com/search.html finds things that are not well documented or seem outdated.

BBEdit has built-in support for XML. There are lots of choices for JSON. I use a multi-language filter, so one key shortcut will format a few languages (that I care about). I'm using prettier as the formatter.

#!/bin/sh

## https://github.com/prettier/prettier
## https://prettier.io
##  ~/.prettierrc.js

# echo $BB_DOC_NAME
# echo $BB_DOC_LANGUAGE

if [ "$BB_DOC_LANGUAGE" == "SCSS" ]; then
	prettier --parser scss
elif [ "$BB_DOC_LANGUAGE" == "JavaScript" ] || [ "$BB_DOC_LANGUAGE" == "HTML" ]; then
	prettier --parser flow
	# prettier --parser babel
elif [ "$BB_DOC_LANGUAGE" == "JSON" ]; then
	prettier --parser json
elif [ "$BB_DOC_LANGUAGE" == "SQL (Generic)" ] || [ "$BB_DOC_LANGUAGE" == "SQL (MySQL)" ]; then
	cli-sql-formatter --dialect sql
else
	echo "unknown language: $BB_DOC_LANGUAGE"
fi

--

Charlie Garrison                   <cha...@garrison.com.au>
Garrison Computer Services      <http://www.garrison.com.au>

@lbutlr

unread,
May 27, 2021, 8:16:27 PM5/27/21
to BBEdit Talk
On 26 May 2021, at 10:41, Arthur Goldberg <artgo...@gmail.com> wrote:
> I'm looking for text filters to format JSON and XML.

Text filters, like regex in BBEdit? Don't. Stop now and back away.

There are good tools for dealing with JSON files (jq is what I use) but there are a lot of formats out there that LOOK like JSON and are not actually valid JSON, so that may not actually work. It's common for me to get "a JSON file" that is not actually a JSON file.

For XML, it depends on what you mean by text-filter. The best XML editor I've used is actually Xcode, though I don't think it is that much better than BBEdit to be worth the 12GB install of all Xcode.

--
'When you've been a wizard as long as I have, my boy, you'll learn
that as soon as you find anything that offers amazing
possibilities for the improvement of the human condition, it's
best to put the lid back on and pretend it never happened.' --The
Last Continent

Greg Raven

unread,
May 28, 2021, 7:24:30 AM5/28/21
to BBEdit Talk
Building on Charlie Garrison's work, this is the one I came up with:

```
#!/bin/sh

## requires pip3 install jsbeautify
## requires pip3 install cssbeautify

## echo $BB_DOC_LANGUAGE

if [ "$BB_DOC_LANGUAGE" == "CSS" ]; then
css-beautify
elif [ "$BB_DOC_LANGUAGE" == "JavaScript" ]; then
js-beautify --jslint-happy
elif [ "$BB_DOC_LANGUAGE" == "JSON" ]; then
python3 -m json.tool
else
echo "unknown language: $BB_DOC_LANGUAGE"
fi
```

Arthur Goldberg

unread,
May 28, 2021, 9:48:53 AM5/28/21
to BBEdit Talk
Thank you everyone for your filter suggestions.

Arthur

Reply all
Reply to author
Forward
0 new messages