Hone logo
Hone
Problems

Implementing a JSON Parser

This challenge asks you to build a rudimentary JSON parser. Parsing JSON is a fundamental task in software development, enabling applications to read and process structured data exchanged over the internet or stored in configuration files. A robust parser can handle complex nested structures and various data types.

Problem Description

Your task is to implement a function that takes a string representing a simplified JSON object as input and converts it into a structured internal representation (e.g., a dictionary/map of key-value pairs, or a similar data structure in your chosen language).

The simplified JSON format will support the following:

  • Objects: Represented by curly braces {} and containing zero or more key-value pairs. Keys are always strings, and values can be strings, numbers, booleans, or nested objects. Key-value pairs are separated by commas.
  • Strings: Enclosed in double quotes "". Special characters within strings are not considered for this challenge.
  • Numbers: Integers (positive or negative) and floating-point numbers.
  • Booleans: true and false.
  • Null: Represented by null.

The parser should:

  1. Correctly identify and parse JSON objects.
  2. Extract keys (which are always strings) and their corresponding values.
  3. Handle nested JSON objects.
  4. Distinguish between different primitive data types (string, number, boolean, null).
  5. Ignore whitespace (spaces, tabs, newlines) outside of string values.

Expected behavior:

  • If the input string is a valid JSON object according to the defined simplified format, return a data structure representing it.
  • If the input string is not a valid JSON object (e.g., malformed syntax), the behavior is undefined for this challenge (you don't need to implement error handling for invalid JSON, but your parser should ideally not crash).

Edge cases to consider:

  • Empty JSON object {}.
  • JSON object with a single key-value pair.
  • JSON object with nested objects.
  • JSON object where keys or values contain spaces (but not within the JSON structure itself, only within string values).
  • JSON object with various primitive types as values.

Examples

Example 1:

Input: "{\n  \"name\": \"Alice\",\n  \"age\": 30,\n  \"isStudent\": false,\n  \"courses\": null\n}"
Output: A data structure equivalent to: {"name": "Alice", "age": 30, "isStudent": false, "courses": null}
Explanation: The input string represents a JSON object with four key-value pairs. The parser should extract each key and its corresponding value, correctly identifying the string, number, boolean, and null types. Whitespace is ignored.

Example 2:

Input: "{\n  \"address\": {\n    \"street\": \"123 Main St\",\n    \"city\": \"Anytown\"\n  },\n  \"isActive\": true\n}"
Output: A data structure equivalent to: {"address": {"street": "123 Main St", "city": "Anytown"}, "isActive": true}
Explanation: This example demonstrates a nested JSON object. The parser needs to recursively handle the inner "address" object, creating a nested data structure.

Example 3:

Input: "{}"
Output: A data structure equivalent to: {}
Explanation: An empty JSON object should be parsed into an empty data structure.

Constraints

  • The input JSON string will be at most 1000 characters long.
  • Keys in JSON objects will always be strings enclosed in double quotes.
  • Values will be of the supported types: strings, numbers, booleans, null, or nested objects.
  • The input will represent a single JSON object at the top level.
  • Floating-point numbers will be represented in standard decimal notation (e.g., 3.14, -0.5).

Notes

  • You will need to handle the parsing of different data types. Consider how you will convert string representations of numbers and booleans into their actual data types.
  • A recursive approach is often suitable for parsing nested structures like JSON objects.
  • Think about how to iterate through the input string and identify tokens (like {, }, :, ,, ", numbers, booleans, null).
  • You do not need to implement escaping of special characters within strings. All string values will be simple.
Loading editor...
plaintext