Difficulty: Easy
Correct Answer: Use JSON.parse(jsonText) to parse the JSON string and return a JavaScript object
Explanation:
Introduction / Context:
In many web applications, data is received from a server as a JSON text string. To work with this data in JavaScript, you need to convert the text into a real JavaScript object or array. Understanding the correct and safe way to perform this conversion is important for both security and correctness. This question checks whether you know the recommended built in method for parsing JSON text.
Given Data / Assumptions:
Concept / Approach:
The JSON global object in JavaScript provides two key methods: JSON.parse and JSON.stringify. JSON.parse takes a JSON formatted string and returns the corresponding JavaScript value, which can be an object, array, string, number, boolean, or null. JSON.stringify does the opposite. Historically, some code used eval to interpret JSON text, but that approach is unsafe because it can execute arbitrary code. JSON.parse is safer and stricter, making it the correct modern choice for converting JSON text into native objects.
Step-by-Step Solution:
Step 1: Identify that we need a function that takes a JSON string as input and outputs a JavaScript value.
Step 2: Recall that JSON.parse is defined exactly for this purpose.
Step 3: Use a statement such as const obj = JSON.parse(jsonText); where jsonText is the JSON string.
Step 4: Confirm that the resulting obj can be accessed with normal dot or bracket notation.
Step 5: Compare with the options and choose option a, which correctly names JSON.parse and describes its behavior.
Verification / Alternative check:
If you run code in a browser console such as const jsonText = "{\"name\":\"Sam\",\"age\":25}"; and then call const user = JSON.parse(jsonText); you can then access user.name and user.age as normal properties. This demonstrates that JSON.parse transforms text into an object. Any attempt to rely on alert, comments, or file extensions will not produce a usable JavaScript object.
Why Other Options Are Wrong:
Option b suggests storing JSON in comments, which are ignored by the parser and never converted into data structures. Option c claims that alert converts strings into objects, but alert only displays text and returns undefined. Option d confuses file type recognition with runtime parsing and does not actually convert JSON. Option e is incorrect because modern JavaScript explicitly supports conversion through JSON.parse.
Common Pitfalls:
A common mistake is to forget that JSON keys must be in double quotes and to pass invalid JSON to JSON.parse, causing exceptions. Another pitfall is to continue using eval on JSON text, which can open security holes if untrusted input is processed. Using JSON.parse consistently and validating the input format helps produce safer and more reliable code.
Final Answer:
To convert JSON text into a JavaScript object, you should call JSON.parse(jsonText), which parses the string and returns the corresponding JavaScript value, as in option a.
Discussion & Comments