JSON is something with which software development engineers work almost every day. Elaborated as, JavaScript Object Notation, JSON is a lightweight text-based format for representing structured data used for transmitting it in web applications. Be it API requests, sending and receiving data from client to server, JSON is almost everywhere. Having worked with JSON.parse() method in JS, I wondered how inner working of such parser could be. So, I tried to explore the building blocks of a JSON parser.
A JSON parser mainly revolves around two concepts:
Lexical Analysis or Tokenization: this is the very first step if you want to make your own compiler or even code prettifier. This process takes the JSON string as input to break into small pieces known as tokens. For example, if the JSON is {“name”:”john”}, the output of the tokenizer would be something like this:
[{type:”curlyBraceOpen”, value:”{”},
{type:”string”, value:”name”},
{type:”colon”, value:”:”},
{type:”string”, value:”john”},
{type:”curlyBraceClose”, value:”}”},
]
Here, you have tokenized the JSON string as an array of token objects by iterating over the string.Syntactic Analysis or Parsing: The parser represents the tokens in a hierarchical tree data structure called AST (Abstract Syntax Tree) holding the logical syntactic structure of the tokenized JSON and validates the string against the standard grammar of the data format being parsed. The tree is formed via iterating the array of tokens depending on the value type.
Finally, after successful validation, you get the valid data in the standard format to access and work with (a valid JS object in this case):
Obj= {
name:”john”
}
Here’s what kept me fascinated along the way:
1.Tokenization is not new nor exclusive to only Natural Language Processing.
2.How data structures and algorithms have always been and will be the foundation behind technologies we see and use every day to shape as a better engineer in any era with paradigm shifts.

