Convert XML to JSON?

381 views
Skip to first unread message

severdia

unread,
Nov 2, 2022, 8:28:42 PM11/2/22
to BBEdit Talk
Is there an easy way to so a simple XML to JSON conversion in BBedit? Oxygen and some other tools seem to do this, but short of creating a TextFactory or similar, I don't see an easy way to do it. 

jj

unread,
Nov 4, 2022, 4:14:32 AM11/4/22
to BBEdit Talk
You could try yq.

 Install it from Terminal with homebrew:

    brew install python-yq
   
Create this simple BBEdit text filter in ~/Library/Application\ Support/BBEdit/Text\ Filters/xml2json.sh
   
    #!/bin/sh
    xq '.'

and call the filter from menu Text > Apply Text Filter > xml2json.

HTH,

Jean Jourdain

severdia

unread,
Nov 4, 2022, 12:28:00 PM11/4/22
to BBEdit Talk
I don't see a menu item called "Apply Text Filter". There's "Apply Text Transform", but when I open that I don't see the script in dialog that opens with that menu. Can you elaborate? Ideally I could process a folder of XML files and convert then to JSON. I'm using BBedit 14.6.1.

Thanks!

jj

unread,
Nov 4, 2022, 1:47:47 PM11/4/22
to BBEdit Talk
Check out that the "Apply Text Filter" checkbox is activated in the Settings > Menus & Sortcuts > Text list:

Screenshot 2022-11-04 at 18.28.00.png
Check out that the shell script has execute permission.

Screenshot 2022-11-04 at 18.33.56.png

Warning: 
The proposed solution "as is" only works for very simple XML.
XML and JSON formats are not homeomorphic, many things can fail : entities, cdata, element repetitions, attributes, etc.
However,  combined with 'jq' there seems to be more powerful customizations.

For a Text Factory to batch a folder refer to the BBEdit manual and the "Run Unix Filter" command.

HTH,

Jean Jourdain

severdia

unread,
Nov 4, 2022, 4:15:53 PM11/4/22
to BBEdit Talk

Excellent. I don't know why that menu items was disabled, but it works now. Also seems to work OK for the XML files I have. Thank you!
Reply all
Reply to author
Forward
0 new messages