Skip to content

Commit

Permalink
Update JsonParsing.md
Browse files Browse the repository at this point in the history
add code blocks; add explanations according to comments
  • Loading branch information
lunaseaa authored Nov 26, 2023
1 parent 5b42947 commit 2ef2e1a
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions Topics/Tech_Stacks/JsonParsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ JSON allows for the construction of more complex data structures beyond primitiv
* Objects: An object in JSON is an unordered collection of key-value pairs. Key-value pairs are separated by commas and enclosed in curly braces {}. Keys must be strings, and values can be strings, numbers, booleans, null, objects, or arrays.
Example:


```{python}
{
"name": "John Doe",
"age": 30,
Expand All @@ -53,10 +53,14 @@ JSON allows for the construction of more complex data structures beyond primitiv
"zipcode": "12345"
}
}
```
In this example, "name", "age", "isStudent", and "address" are keys, and their corresponding values are strings, numbers, boolean, and another object, respectively.


* Array: An array in JSON is an ordered list of values. Values are separated by commas and enclosed in square brackets []. Values can be strings, numbers, booleans, null, objects, or other arrays.
Example:

```{python}
[
"apple",
"banana",
Expand All @@ -66,6 +70,33 @@ JSON allows for the construction of more complex data structures beyond primitiv
"quantity": 5
}
]
```
In this example, the array contains strings ("apple", "banana", "orange") and an object with keys "color" and "quantity".

JSON structures often combine objects and arrays to represent more complex data hierarchies. For instance, an array of objects can represent a collection of similar entities, where each object has multiple key-value pairs.

```{python}
[
{
"name": "Alice",
"age": 25,
"isStudent": true
},
{
"name": "Bob",
"age": 30,
"isStudent": false
},
{
"name": "Charlie",
"age": 22,
"isStudent": true
}
]
```

In this example, the array contains three objects, each representing a person with attributes such as name, age, and student status.


## JSON Parsing in Different Programming Languages

Expand Down Expand Up @@ -102,7 +133,7 @@ Suppose we have a JSON file called fcc.json. If we want to read that file, we fi
```{python}
with open('fcc.json', 'r') as fcc_file:
```
We can then parse the file using the json.load() method and assign it to a variable called fcc_data.
We can then parse the file using the `json.load()` method and assign it to a variable called fcc_data.

```{python}
fcc_data = json.load(fcc_file)
Expand All @@ -113,6 +144,17 @@ The final step would be to print the results.
print(fcc_data)
```

This is what the entire code would look like:
```{python}
import json
with open('fcc.json', 'r') as fcc_file:
fcc_data = json.load(fcc_file)
print(fcc_data)
```



### JavaScript Parse JSON

#### Parse JSON String in JavaScript
Expand Down Expand Up @@ -215,6 +257,8 @@ Name: Kotte
College: BVRIT
```

In the above program, the `JSONParser().parse()` is used, which is present in the `org.json.simple.parser.*` to parse the File.json file.

## Parsing Optimization

JSON parse optimization is crucial for achieving optimal performance, resource efficiency, and a seamless user experience in applications that handle JSON data. It becomes particularly relevant in scenarios involving large datasets, real-time updates, and applications with high concurrency and scalability requirements. Performance optimization in the context of JSON involves strategic measures to enhance the efficiency of handling and transmitting JSON data. This includes focusing on two key aspects:
Expand Down

0 comments on commit 2ef2e1a

Please sign in to comment.