You are given a valid JSON string jsonString
. Your task is to convert it into a JavaScript object.
Example 1:
Input: jsonString = '{"name":"John", "age":30, "city":"New York"}'
Output: {"name":"John", "age":30, "city":"New York"}
Explanation: The JSON string is successfully converted into a JavaScript object.
Example 2:
Input: jsonString = '[1, 2, 3, 4, 5]'
Output: [1, 2, 3, 4, 5]
Explanation: The JSON string representing an array is correctly converted into a JavaScript array.
Example 3:
Input: jsonString = '{"success": true, "message": "Operation completed successfully"}'
Output: {"success": true, "message": "Operation completed successfully"}
Explanation: A more complex JSON with boolean and string values is parsed without issues.
Constraints:
jsonString
is a valid JSON string.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to converting a JSON string to an object involves trying all possible interpretations of the JSON structure. We explore every potential arrangement of key-value pairs and nested objects until we find one that is valid and represents the data accurately. This is like trying every possible combination until something fits.
Here's how the algorithm would work step-by-step:
def convert_json_to_object_brute_force(json_string):
index = 0
json_length = len(json_string)
def parse_value():
nonlocal index
while index < json_length and json_string[index].isspace():
index += 1
if index >= json_length:
return None, False
current_char = json_string[index]
if current_char == '"':
index += 1
string_start = index
while index < json_length and json_string[index] != '"':
index += 1
string_end = index
if index >= json_length:
return None, False
index += 1
return json_string[string_start:string_end], True
elif current_char.isdigit() or current_char == '-':
number_start = index
while index < json_length and (json_string[index].isdigit() or json_string[index] == '.' or json_string[index] == '-'):
index += 1
number_string = json_string[number_start:index]
try:
return int(number_string), True
except ValueError:
try:
return float(number_string), True
except ValueError:
return None, False
elif current_char == '{':
object_value, success = parse_object()
return object_value, success
elif current_char == '[':
array_value, success = parse_array()
return array_value, success
elif json_string[index:index+4] == 'true':
index += 4
return True, True
elif json_string[index:index+5] == 'false':
index += 5
return False, True
elif json_string[index:index+4] == 'null':
index += 4
return None, True
else:
return None, False
def parse_array():
nonlocal index
index += 1
array_result = []
while index < json_length:
value, success = parse_value()
if not success:
return None, False
array_result.append(value)
while index < json_length and json_string[index].isspace():
index += 1
if index < json_length and json_string[index] == ',':
index += 1
elif index < json_length and json_string[index] == ']':
index += 1
return array_result, True
else:
return None, False
return None, False
def parse_object():
nonlocal index
index += 1
object_result = {}
while index < json_length:
while index < json_length and json_string[index].isspace():
index += 1
key, success = parse_value()
if not success:
return None, False
while index < json_length and json_string[index].isspace():
index += 1
if index >= json_length or json_string[index] != ':':
return None, False
index += 1
value, success = parse_value()
if not success:
return None, False
object_result[key] = value
while index < json_length and json_string[index].isspace():
index += 1
if index < json_length and json_string[index] == ',':
index += 1
elif index < json_length and json_string[index] == '}':
index += 1
return object_result, True
else:
return None, False
return None, False
while index < json_length and json_string[index].isspace():
index += 1
if index < json_length and json_string[index] == '{':
# JSON string starts with an object
result, success = parse_object()
if success:
return result
else:
return None
elif index < json_length and json_string[index] == '[':
# JSON string starts with an array
result, success = parse_array()
if success:
return result
else:
return None
else:
# Handle other cases or errors
return None
The best way to turn a text-based JSON into a usable object is to carefully read it piece by piece. We analyze each character and use that to build the structure of the object step by step, recognizing data types and relationships as we go.
Here's how the algorithm would work step-by-step:
import re
class Solution:
def convert_json_string_to_object(self, json_string: str):
if json_string is None or json_string.strip() == "":
return {}
json_string = json_string.strip()
if json_string.startswith("{"):
return self.parse_json_object(json_string)
elif json_string.startswith("["):
return self.parse_json_array(json_string)
else:
return self.parse_json_primitive(json_string)
def parse_json_object(self, json_string: str):
obj = {}
json_string = json_string[1:-1].strip() # remove { }
if not json_string:
return obj
# Split key-value pairs by commas not inside quotes
key_value_pairs = re.split(r',(?=(?:[^"]*"[^"]*")*[^"]*$)', json_string)
for pair in key_value_pairs:
parts = pair.split(":", 1)
key = parts[0].replace('"', '').strip()
value = parts[1].strip()
# Recursively convert the value
obj[key] = self.convert_json_string_to_object(value)
return obj
def parse_json_array(self, json_string: str):
arr = []
json_string = json_string[1:-1].strip() # remove [ ]
if not json_string:
return arr
# Split elements by commas not inside quotes
elements = re.split(r',(?=(?:[^"]*"[^"]*")*[^"]*$)', json_string)
for element in elements:
arr.append(self.convert_json_string_to_object(element.strip()))
return arr
def parse_json_primitive(self, json_string: str):
if json_string == "null":
return None
if json_string == "true":
return True
if json_string == "false":
return False
if json_string.startswith('"') and json_string.endswith('"'):
return json_string[1:-1]
# Try parsing as number
try:
if "." in json_string or "e" in json_string or "E" in json_string:
return float(json_string)
return int(json_string)
except ValueError:
return json_string
Case | How to Handle |
---|---|
Null or empty JSON string input | Return None or raise a ValueError indicating invalid input, depending on the desired behavior. |
JSON string contains only whitespace | Remove whitespace and check if the resulting string is empty; handle as null input. |
JSON string with invalid syntax (e.g., missing quotes, unmatched brackets) | Raise a ValueError or SyntaxError during parsing to indicate an invalid JSON format. |
JSON string contains numbers exceeding maximum integer value or floating point precision. | The Python interpreter will automatically handle integers and floats without specific intervention, potentially losing precision on extremely large numbers. |
JSON string with deeply nested objects and arrays causing recursion depth issues. | Consider iterative parsing or increase recursion limit if needed, but also limit allowed depth to prevent stack overflow. |
JSON string contains duplicate keys in objects | The parser should use the last occurrence of the key, overwriting previous values (standard JSON behavior). |
JSON string with non-ASCII characters in strings. | Ensure the string is decoded using UTF-8 or appropriate encoding to handle unicode correctly. |
JSON string representing a single primitive value (e.g., '123', 'true', 'null', '"hello"') | Return the corresponding Python primitive value directly without requiring a top-level object or array. |