JSON - Java Script Object Notation is a light weight data format to transfer the data between server and client. Like XML JSON is also a text based data format. In this post we will cover what is parsing in JSON and how to parse JSON in javascript with example.
JSON Parsing is data in the string format with specified fields, this string is in the form of JSON which has some specialized mechanism to arrange the data. This JSON has 2 structures
JSON Object which is an unordered key, value pair data, which is starts with an open curly bracket '{' and end with close curly bracket '}'
JSON Array which is an ordered list of values, it may contains list of JSON objects, strings... This array begins with an left bracket '[' and end with right bracket ']'
Simple JSON Object
{ "option_1": "Macrograph", "option_2": "Nanographia", "option_3": "Mycographia", "option_4": "Micrographia" } |
Simple JSON Array
[ { "option_1": "Macrograph", "option_2": "Nanographia", "option_3": "Mycographia", "option_4": "Micrographia" }, { "option_1": "Macrograph", "option_2": "Nanographia", "option_3": "Mycographia", "option_4": "Micrographia" }, { "option_1": "Macrograph", "option_2": "Nanographia", "option_3": "Mycographia", "option_4": "Micrographia" } ] |
How to parse JSON in Javascript?
In Javascript we can easily parse the JSON string received from server by using JSON.parse() method. This method parse the given string into Javascript Object. If the given string is not a proper JSON format then it will return error.
Simple JSON String parser example
<!DOCTYPE html> let jsonObj=JSON.parse(jsonStr); |
When we run the above example in the console it will print the JSON Object value
We can remove/delete an existing property from JSON
<script> let jsonObj=JSON.parse(jsonStr, (key, value) => { |
It will remove the option_2
output:
Article Contributed By :
|
|
|
|
942 Views |