Hone logo
Hone
Problems

Go JSON Parser Challenge

This challenge focuses on building a fundamental JSON parser in Go. Understanding how to parse structured data like JSON is a crucial skill for any Go developer, enabling you to work with APIs, configuration files, and data storage. You'll gain hands-on experience with string manipulation, recursion, and handling different data types.

Problem Description

Your task is to create a function in Go that takes a string representing a JSON document and parses it into a Go interface{}. This interface{} should represent the parsed JSON structure, allowing for nested objects and arrays.

Key Requirements:

  • Support for Basic JSON Types: The parser must correctly handle:
    • Objects: {"key": "value", "number": 123}
    • Arrays: [1, "hello", true]
    • Strings: "a string"
    • Numbers: 123, 3.14, -50
    • Booleans: true, false
    • Null: null
  • Recursive Parsing: The parser needs to handle nested JSON structures (objects within objects, arrays within objects, etc.) through recursion.
  • Error Handling: The parser should return an error if the input string is not valid JSON.
  • Output Type: The parsed JSON should be returned as a map[string]interface{} for JSON objects and []interface{} for JSON arrays. Primitive types (string, number, boolean, nil) should be represented by their corresponding Go types.

Expected Behavior:

Given a valid JSON string, the function should return a Go data structure that accurately reflects the JSON's hierarchy and values. For invalid JSON, it should return an error.

Edge Cases to Consider:

  • Empty JSON object: {}
  • Empty JSON array: []
  • JSON with escaped characters within strings (e.g., \", \\, \n).
  • Numbers with exponents (e.g., 1e6).
  • Leading/trailing whitespace around JSON elements.
  • Invalid JSON syntax (e.g., missing commas, incorrect quotes).

Examples

Example 1:

Input: `{"name": "Hone", "age": 5, "isAI": true, "hobbies": ["coding", "learning"], "address": {"city": "Gopherland", "zip": null}}`

Output:
map[string]interface{}{
    "name": "Hone",
    "age": 5,
    "isAI": true,
    "hobbies": []interface{}{"coding", "learning"},
    "address": map[string]interface{}{
        "city": "Gopherland",
        "zip": nil,
    },
}

Explanation: The input JSON object is parsed into a map. String, number, boolean, array, and nested object types are correctly identified and represented. The `null` value is parsed as `nil`.

Example 2:

Input: `[1.23, "test", false, {"nested": [{}, []]}]`

Output:
[]interface{}{
    1.23,
    "test",
    false,
    map[string]interface{}{
        "nested": []interface{}{
            map[string]interface{}{},
            []interface{}{},
        },
    },
}

Explanation: The input JSON array is parsed into a slice. Mixed types within the array, including a nested object with empty array and object, are handled correctly.

Example 3:

Input: `{"key_with_escaped_quote": "this is a \"quoted\" string"}`

Output:
map[string]interface{}{
    "key_with_escaped_quote": "this is a \"quoted\" string",
}

Explanation: The parser correctly interprets the escaped double quote within the string.

Example 4 (Invalid JSON):

Input: `{"name": "Hone", "age": 5,}` // Trailing comma

Output: An error indicating invalid JSON syntax.

Explanation: Most JSON parsers do not allow trailing commas in objects or arrays.

Constraints

  • The input JSON string will be at most 10,000 characters long.
  • The parser should handle standard JSON data types as described.
  • The parsing process should aim for reasonable efficiency, avoiding unnecessary string copying where possible.

Notes

  • You are not expected to implement a lexer and parser from scratch in the traditional compiler sense (e.g., using grammar rules and ASTs). You can use string manipulation and recursive function calls to traverse and interpret the JSON structure.
  • Consider how you will handle different number formats (integers, floats, scientific notation).
  • Pay close attention to the precise definition of JSON syntax, especially around delimiters (,, :, [, ], {, }), quotes, and whitespace.
  • You may want to create helper functions to parse individual JSON values (strings, numbers, objects, arrays).
  • The strconv package in Go can be very useful for converting strings to numbers and booleans.
  • This challenge is about understanding JSON structure and implementing parsing logic. You are free to use standard library features, but you should not use a pre-built JSON parsing library like encoding/json.
Loading editor...
go