Detecting Quality

Mastering JSON

Detecting the Quality

Having a different day, I faced a new challenge - an interview in a non-native language. Although it may seem trivial to experts, I realized I had a lot to say but struggled to find the right words. This experience highlighted the importance of improving my language skills, and I’ve identified key areas to focus on. Note the title is not related to the content of this post.

JSON: Data Interchange Format

I’ve worked extensively with JSON, but I struggled to explain it. Let me try to break it down.

JSON (JavaScript Object Notation) is a text-based format used to send data from a server to a web page. Its simplicity and versatility make it an ideal choice for data exchange between different programming languages. JSON is often seen as an alternative to XML.

JSON vs. XML: Key Differences

Both JSON and XML are data interchange formats, but they have distinct differences. JSON is more compact and lightweight, whereas XML supports comments and namespaces, allowing for more flexibility in data representation. XML also carries more metadata due to its extensive markup abilities.

JSON in JavaScript: Parsing and Conversion

JSON serves as a common medium for data exchange between different languages. In JavaScript, JSON data can be accessed through parsing using the JSON.parse() method, which converts a JSON string into a JavaScript object.

1
let jsonObject = JSON.parse(jsonString);

JSON Types in JavaScript

JSON arrays are ideal for collections where order matters.

1
2
myJSON = '["Ford", "BMW", "Fiat"]';
myArray = JSON.parse(myJSON);

JSON objects, on the other hand, contain key-value pairs, where keys and values are separated by a colon.

1
{"name":"John", "age":30, "car":null}

String to JSON

When sending data to a web server, it must be converted to a string using the JSON.stringify() method.

1
2
const obj = {name: "John", age: 30, city: "New York"};
const myJSON = JSON.stringify(obj);

Dealing with Errors

Error handling is crucial when parsing JSON to ensure data integrity and application stability. Using try-catch blocks is the primary approach.

1
2
3
4
5
6
7
8
9
if (response) {
    let a;
    try {
        a = JSON.parse(response);
    } catch (e) {
        return console.error(e); // error in the above string (in this case, yes)!
    }
    // if no error, we can now keep using "a"
}

A Messaging Protocol for Web Services …. SOAP

SOAP (Simple Object Access Protocol) is a messaging protocol used to invoke web services over networks. It relies on XML as its message format. SOAP was designed to facilitate communication between applications over HTTP, which is supported by all internet browsers and servers.

A Markup Language for Data Storage and Transport… XML

XML (eXtensible Markup Language) is a markup language designed to store and transport data. It’s both human- and machine-readable, making it an ideal choice for data exchange.

Writing this post has helped me to reflect on my knowledge and identify areas for improvement. Every day is an opportunity to learn and grow.

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy