
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
json-grammar
Advanced tools
JSON Grammar, or JSG, is a language for describing the structure of JSON documents. It can be used for documentation, describing what a service or tool consumes or emits, and validation, testing conformance of some data to that description.
See a simple online demo.
A JSG schema is composed of objects, rules and values.
Objects are represented by a production name followed by a "{", some named members or rule references, and "}".
These describe JSON objects like { "street":"Elm", "number":"123b" }.
A member is composed of an attribute name, a ":", and a type: { "street":NAME, "number":NUMBER }.
A type can be a constant, value pattern, rule name, or a list of types:
By convention, value patterns labeled with ALL CAPS.
| JSON Grammar | matching JSON | |
|---|---|---|
doc { status:"ready" } |
| |
doc { street:NAME no:NUM }
NAME : .*;
NUM : [0-9]+[a-e]?; |
| |
doc { street:(NAME|"*"|TEMPLATE) }
NAME : .*;
TEMPLATE : '{' .* '}'; |
| |
doc { street:nameOrTemplate }
nameOrTemplate = NAME | "*" | TEMPLATE
NAME : .*;
TEMPLATE : '{' .* '}'; |
| |
doc { street:[nameOrTemplate{2,} }
nameOrTemplate = NAME | "*" | TEMPLATE
NAME : .*;
TEMPLATE : '{' .* '}'; |
|
A schema can be composed with no rules but rule names can help with:
Values are represented by a terminal name followed by a ":" and a regular pattern (c.f. lex) nad a ";". They can reference each other (but not circularly) allowing a value to be composed of other values. The syntax is reminiscent of EBNF or W3C language specifications, e.g.:
| Value pattern | matching JSON | |
|---|---|---|
'@' START+ ('-' MIDCHAR+)*
START : [a-zA-Z];
MIDCHAR : START | [0-9];
NUM : [0-9]+[a-e]?; |
|
Code points in values can be specified by:
'-', "x-", '"'),[a-z])\u" followed by a hexidecimal numeric unicode code point. These can appear in quoted strings and character ranges.If we had a disdain for writing the letter 'a' and the symbol '@', we could write the above value pattern as:
\u0040 START+ ('-' MIDCHAR+)*
START : [\u0061-z\u0041-Z];
MIDCHAR : START | [0-9];
NUM : [0-9]+[\u0061-e]?;
| JSON Grammar | JSON | |
|---|---|---|
doc { a:STRING } STRING=".*" | passes | { "a":"hi" } |
doc { a:STRING } STRING=".*" | fails | { "type":"doc", "a":"hi" } |
.IGNORE type; doc { a:STRING } STRING=".*" | passes | { "type":"doc", "a":"hi" } |
doc { a:STRING, type:STRING } STRING=".*" | passes | { "type":"doc", "a":"hi" } |
.TYPE type; doc { a:STRING } STRING=".*" | passes | { "type":"doc", "a":"hi" } |
.TYPE type; doc { a:STRING } STRING=".*" | fails | { "type":"docXXX", "a":"hi" } |
| You can push the .TYPE property into each object if you want (and have to if it's not universal). | ||
| Error reports on schemas with a .TYPE directive tend to be terser as failing a discriminator check shortcuts the tests of all the other object properties. |
All PRs welcome. Please run tests first:
JSG has a set of built-in tests. It also tests JSON structures from the ShEx and SPARQL.js repositories. This presumes specific paths between where these are checked out. You can accomplish this by checking everything out in a directory, e.g. github:
mkdir github
cd github
git clone git@github.com:shexSpec/shexTest shexSpec/shexTest
git clone git@github.com:RubenVerborgh/SPARQL.js RubenVerborgh/SPARQL.js
# now to get JSG, initialize it and run the tests:
git clone git@github.com:ericprud/jsg ericprud/jsg
cd ericprud/jsg
npm install
npm run test-all
test/test.js has an easy way to enter passing and failing tests, e.g.
["ShExJ.jsg", "empty.json", true],
["ShExJ.jsg", "bad-noType.json", "type"],
["ShExJ.jsg", "bad-wrongType.json", false],
which tests that empty.json passes, bad-noType.json fails with an error mentioning "type" and bad-wrongType.json fails for some reason.
FAQs
Grammar-based description and validation of JSON structures.
The npm package json-grammar receives a total of 7 weekly downloads. As such, json-grammar popularity was classified as not popular.
We found that json-grammar demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.