How to fix Error: 767: unexpected token at '' from Homebrew command

X anonymous
3 min readJul 30, 2022

--

For some unknown reasons, homebrew on my mac has been throwing up this error since one day.

When I tried to fix it, I first made sure my homebrew files was completely fine, so I moved into the homebrew repository folder and confirmed it with git:

Next, I thought about reinstalling homebrew.

homebrew doesn't provide a way to reinstall, so we can only do this by uninstalling it and installing it again.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

https://github.com/homebrew/install#uninstall-homebrew

but when I did, it stopped me with a warning:

So I started trying to fix the problem without reinstalling.

Then I looked at that error message, and I noticed that it seemed to be an error while parsing some json content.

Let's modify the ruby code and print the json content to be parsed to see what happened.

As the error message says, I found the JSON.parse(json) action on line 65 in the /usr/local/Homebrew/Library/Homebrew/cask/config.rb file, so I added a p json just before that, to print the content of json.

After modifying the save file and closing it, I executed the brew upgrade command again, and I saw this:

Now we can clearly know that a json with strange content (a lot of \u0000) was parsed and caused this error.

In order to find out which json file has these strange contents, we can see that there is a config_path variable in line 53 of /usr/local/Homebrew/Library/Homebrew/cask/cask.rb, let’s add a few lines code and print it.

Now call brew upgrade again to let it show the error.

And then we caught the file of the problem!

At this point, I think you should know what to do: try to delete the problem file, or fix it.
In my case it belonged to a package called swiftformat-for-xcode, so I tried searching for this package and found information about it on brew's website.
https://formulae.brew.sh/cask/swiftformat-for-xcode

So I modified the content of /usr/local/Caskroom/swiftformat-for-xcode/.metadata/config.json, deleted all the strange strings, leaving only an empty {} and then used the brew command to uninstall it.

And the brew will now work without errors since we have fixed that problem file :)

--

--