Minified JSON in VCL
In this post, I want to share my experience with synthetic responses in JSON
. You can set up your Fastly service configuration to serve a custom page or a synthetic response for a specific status code from your backend. You have the choice between different MIME Types
like application/json
, text/plain
, or text/html
for example. Fastly provides great documentation on how to set up a synthetic response which can be found here.
Problem
The problem is that the Fastly VCL compiler throws a compilation error for minified JSON
objects. The reason is that Varnish doesn’t like nested {" "}
sequences even if it is a valid JSON
object. Minification is the process of removing all white space and new line characters from a JSON
object.
There are many free tools online to minify JSON
objects. Here are a few examples:
- codebeautify
- browserling
- webtoolkitonline
Example
{
"errors": [
{
"source": { "parameter": "include" },
"title": "Invalid Query Parameter",
"detail": "The resource does not have an `auther` relationship path."
}
]
}
The above non-minified valid JSON
object compiles. But if you minify it to
{"errors":[{"source":{"parameter":"include"},"title":"Invalid Query Parameter","detail":"The resource does not have an `auther` relationship path."}]}
it will fail even if it is valid JSON
! The compiler will close the string whenever it finds the first "}
sequence and doesn’t respect that you have another {"
sequence opened.
Solution
-
Don’t minify your
JSON
objects! I don’t think it is necessary to minify smallJSON
objects like the example. There shouldn’t be any performance differences. Of course, it makes sense to minify largeJSON
objects to decrease the payload size of the response and therefore the performance. -
Use the
Find and Replace
feature of your preferred text editor and replace all"}
sequences with"}{"""}{"}"}{"
to escape the compiler’s behavior. The minified example object will compile if you update it to:
{"errors":[{"source":{"parameter":"include"}{"""}{"}"}{","title":"Invalid Query Parameter","detail":"The resource does not have an `auther` relationship path."}{"""}{"}"}{"]}
Conclusion
Be aware if you set up a synthetic response on Fastly with a MIME Type
of application/json
. If you haven’t minified the JSON
object then you shouldn’t run into any compilation errors assumed it is valid JSON
. If you have a large JSON
object it makes sense to minify it. There are many free compressing tools online which help you with the transformation. In order to use minified JSON
objects in VCL you’ll need to replace all "}
sequences with "}{"""}{"}"}{"
.
Please sign in to leave a comment.
Comments
0 comments