Hunting for JSON

Luis Lozano
3 min readApr 15, 2021

--

Not that Jason! Its JSON (Jay-sawn) which stands for JavaScript Object Notation. Even though it may not pack a punch quite like Jason Bourne can, JSON can be just as versatile. Some of the things we can use JSON for include storing, exchanging, sending, and receiving data. Its format is text only so while it uses JavaScript syntax, it can be read and used by any programming language. While accessing data in JSON can get much more complicated, this beginner’s guide will help get you started. To begin, lets look at what JSON looks like. Below is an example:

example of JSON

As seen above, there are a few rules to JSON’s syntax. They are that data should be in name and value pairs, data is separated by commas, objects should be held in curly braces, and as always, arrays are held in square brackets. The data types that can be used with JSON include strings (JSON format requires double quotes!), numbers, objects, arrays, Booleans, and null.

Retrieving Info from JSON Objects

JSON object values are typically accessed using dot (.) notation or with brackets. Here we set the object to a variable named assassin then declare that to a variable called x so we can access it:

Accessing object values.

We can access JSON objects by looping through them using a for-in loop. Assuming we have an element in our html file with an id set to ”asset-info”, we can get properties or values about our assassin like so:

For-in loop returns our properties.
For-in loop returns our values.

Nested JSON objects can be accessed just as easily using either dot notation or bracket notation:

Accessing data from nested JSON objects.

Retrieving Info from JSON Arrays

Arrays in JSON are accessed like your average array. The most basic way of course is using its index number in brackets. Below is an example of using the array’s index to retrieve a single value and retrieving all values using a for loop.

Accessing data from an array using its index and a for loop.

Using JSON to grab data can seem confusing at first. As with most things, learning the basics will go a long way to help build our skills. A lot of times complicated things can be broken down into the basics and will turn out to not be that difficult to solve after all. I hope this guide will help someone out. Happy coding!

--

--

Luis Lozano
Luis Lozano

No responses yet