Can anyone help with Script that formats and replaces JSON?

87 views
Skip to first unread message

Ken Corey

unread,
Jan 29, 2020, 8:46:09 AM1/29/20
to BBEdit Talk
Hi All,

I have a script, shown below, that allows me to put the cursor on a single line of json and call my script.  A terminal window is opened, and the formatted json is shown.  I then can copy it out and paste it back into the document.

It works, but it's still a bit of a bother.

I'd like the json line in BBEdit to be replaced with the formatted json.  No terminal window needed.

Currently, the script uses the 'jq' tool from here: https://stedolan.github.io/jq/

----------Script jqpretty------------------

tell application "BBEdit"

set the_selection to (selection of front window as string)

if (the_selection) is "" then

set the_selection to line (get startLine of selection) of front window as string

end if

end tell


tell application "Terminal"

do script with command "echo '" & the_selection & "' | jq ." in window 1

end tell

----------------------------------------------

Thanks!

-Ken

GP

unread,
Jan 29, 2020, 2:14:29 PM1/29/20
to BBEdit Talk


On Wednesday, January 29, 2020 at 5:46:09 AM UTC-8, Ken Corey wrote:
Hi All,

I have a script, shown below, that allows me to put the cursor on a single line of json and call my script.  A terminal window is opened, and the formatted json is shown.  I then can copy it out and paste it back into the document.

It works, but it's still a bit of a bother.

I'd like the json line in BBEdit to be replaced with the formatted json.  No terminal window needed.


Something like:

set the_formatted to do shell script "echo  & the_selection &  | jq ."

will eliminate the need to use the Terminal application.

Something like the following quick and dirty hack will probably work:

tell application "BBEdit"
set the_selection to (selection of front window as string)
if (the_selection) is "" then
set the_selection to line (get startLine of selection) of front window as string
set the_formatted to do shell script "echo  & the_selection &  | jq ."
set (line (get startLine of selection) of front window) to the_formatted
else
set the_formatted to do shell script "echo  & the_selection &  | jq ."
set (selection of front window) to the_formatted
end if
end tell

jj

unread,
Jan 30, 2020, 7:09:02 AM1/30/20
to BBEdit Talk
Hi,

Here is my take on a script to apply jq on a selection in BBEdit.
Use at your own risks, it has not been thoroughly tested.


Best regards.

Jean Jourdain

Ken Corey

unread,
Jan 31, 2020, 11:33:39 AM1/31/20
to BBEdit Talk
And yours handles escaping and even has error handling in the script.  

That's brilliant.  I'm going to have to start putting the stuff in my scripts.

Thank you!

-Ken

Ken Corey

unread,
Jan 31, 2020, 11:33:39 AM1/31/20
to BBEdit Talk
That's top notch.  I had one small thing where I needed to protect the contents of the string to get it to work:

tell application "BBEdit"

set the_selection to (selection of front window as string)

if (the_selection) is "" then

set the_selection to line (get startLine of selection) of front window as string

set the_formatted to do shell script "echo '" & the_selection & "' | /usr/local/bin/jq ."

set (line (get startLine of selection) of front window) to the_formatted

else

set the_formatted to do shell script "echo '" & the_selection & "' | /usr/local/bin/jq ."

set (selection of front window) to the_formatted

end if

end tell


Thank you so much!


-Ken

Jagrut Sharma

unread,
Sep 17, 2021, 2:26:44 PM9/17/21
to BBEdit Talk
Thank you. This was helpful.
--
Jagrut

jj

unread,
Sep 17, 2021, 3:31:45 PM9/17/21
to BBEdit Talk

Revisiting this thread.
Provided jq in in your PATH, my convoluted applescript script could be replaced by this simple text filter:

#!/bin/sh
jq '.'

Zen and the Art of BBEdit Script Maintenance.

Regards,

Jean Jourdain
Reply all
Reply to author
Forward
0 new messages