You shouldn't need to write code for this.
From what I understand, in your case cached .json files are not invalidated on the dispatcher; that's part of the default configuration, because even .json files are not cached. You need to explicitly configure the dispatcher to invalidate .json files as well.
The next problem could be the statfilelevel. That's a single value which applies to all invalidation requests, independent of path (which can be a problem in your case). Let me explain that with an example:
You have
* /content/brand/mysite/en/page
* /content/brand/mysite/es/homepage
* /content/brand/mysite/es/products
Then you would probably set the statfilelevel to to 4, so any change on the es site purges only the cache of the es-site and not on the en site.
That can be problematic when you have as well
*/etc/tags/en/tag1
*/etc/tags/es/tag1
*/etc/tags/es/tag2
Because now any change on /etc/tags/es/tag2 will not cause the all tags of es being flushed.
But please check before that you configured the invalidation of the .json files.