Introduction to the JSON data structure (2024)


Learning Objectives

After completing this tutorial, you will be able to:

  • Describe the key structure elements of a json file: object names and values.
  • List some of the core data types that json files can store including: boolean, numeric and string.
  • Identify the components of the hierarchical json structures including: objects, arrays and data elements.

What you need

You will need a computer with internet access to complete this lesson.

In the previous lesson, you learned how to access human readable text files data programmatically using:

  1. download.file() to download a file to your computer and work with it (ideal if you want to save a copy of the data to your computer)
  2. read.csv() ideal for reading in a tabular file stored on the web but may sometimes fail when there are secure connections involved (e.g. https).

You also learned how to use getURL() for urls that are both secure (https) and less secure (http).

In this lesson, you will learn about API interfaces. An API allows us to access data stored on a computer or server using a specific query. APIs are powerful ways to access data and more specifically the specific type and subset of data that you need for your analysis, programmatically.

You will also explore the machine readable JSON data structure. Machine readable data structures are more efficient - particularly for larger data that contain hierarchical structures. In this lesson, the getURL() function will become more valuable to us as you parse data accessed from an API.

#NOTE: if you have problems with ggmap, try to install both ggplot and ggmap from github#devtools::install_github("dkahle/ggmap")#devtools::install_github("hadley/ggplot2")library(ggmap)library(ggplot2)library(dplyr)library(rjson)

Review

Remember that in the first lesson in this module, you learned about RESTful APIs. You explored the concept of a request and then a subsequent response. The request to an RESTful API is composed of a URL and the associated parameters required to access a particular subset of the data that you wish to access.

When you send the request, the web API returns one of the following:

  1. The data that you requested or
  2. A failed to return message which tells us that something was wrong with your request.

About JSON

Before we go any further, let’s take a moment to revisit the JavaScript Object Notation or JSON data structure that reviewed in the introductory lesson in this module. JSON is an ideal format for larger data that have a hierarchical structured relationship.

The structure of a JSON object is as follows:

  • The data are in name/value pairs
  • Data objects are separated by commas
  • Curly braces {} hold objects
  • Square brackets [] hold arrays
  • Each data element is enclosed with quotes "" if it is a character, or without quotes if it is a numeric value
 { "name":"Chaya" }
{ "name":"Chaya", "age":55, "city":"Boulder", "type":"Canine" }

Notice that the data above are structured. Thus, each element contains a particular object name. (name, age, city, etc). This is similar to column headings in a .csv. However, the JSON structure can also be nested. Like this:

{"students":[ { "firstName":"Serena", "lastName":"Williams" }, { "firstName":"Boe", "lastName":"Diddly" }, { "firstName":"Al", "lastName":"Gore" }]}

The ability to store nested or hierarchical data within a text file structure makes JSON a powerful format to use as you are working with larger datasets.

Data Tip: The GEOJSON data structure is a powerful data structure that supports spatial data. GEOJSON can be used to create maps just like shapefiles can. This format is often used for web mapping applications like leaflet (which you will learn about later in this module).

JSON Data Structures

JSON can store any of the following data types:

  • strings
  • numbers
  • objects (JSON object)
  • arrays
  • booleans (TRUE / FALSE)
  • null

Note that in the example below - the word “Chaya” which is the value associated with name is in quotes "". This specifies that chaya is a string (characters rather than numeric).

 { "name":"Chaya" }

In this example, the value 55 associated with age, is not in quotes. This specifies that this value is a number or of type numeric.

{ "name":"Chaya", "age":55, "city":"Boulder", "type":"Canine" }

In the next lesson, you will work with JSON structued data, accessed via an RESTful API. A snippet of the data that you will work with is below.

[{"age":"0","county":"Adams","datatype":"Estimate","femalepopulation":"2404","fipscode":"1","malepopulation":"2354","totalpopulation":"4758","year":"1990"},{"age":"1","county":"Adams","datatype":"Estimate","femalepopulation":"2375","fipscode":"1","malepopulation":"2345","totalpopulation":"4720","year":"1990"},{"age":"2","county":"Adams","datatype":"Estimate","femalepopulation":"2219","fipscode":"1","malepopulation":"2413","totalpopulation":"4632","year":"1990"}
Introduction to the JSON data structure (2024)

FAQs

Introduction to the JSON data structure? ›

JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines seven value types: string, number, object, array, true, false, and null. The following example shows JSON data for a sample object that contains name-value pairs.

What is the data structure of JSON? ›

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

How to define structure in JSON? ›

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma.

What is the key structure of JSON? ›

Key-value pairs have a colon between them as in "key" : "value" . Each key-value pair is separated by a comma, so the middle of a JSON lists as follows: "key" : "value", "key" : "value", "key": "value" . In the previous example, the first key-value pair is "first_name" : "Sammy" .

What is the standard JSON structure? ›

A JSON array is represented by brackets ( [ , ] ) surrounding the representations of the array elements (also called items), which are separated by commas ( , ), and each of which is an object, an array, or a scalar value. Array element order is significant.

Is JSON fully structured data? ›

JavaScript Object Notation (JSON) is an open-standard data format or interchange for semi-structured data. It is text-based and readable by humans and machines.

What are two main structures compose JSON? ›

The two primary parts that make up JSON are keys and values. Together they make a key/value pair. Key: A key is always a string enclosed in quotation marks. Value: A value can be a string, number, boolean expression, array, or object.

Is JSON difficult to learn? ›

Compared to XML and other data interchange formats, JSON is much easier for humans to write and understand. It's also specifically designed for use as a data interchange format, whereas XML was originally a markup language.

Is JSON structured or unstructured? ›

Examples of semi-structured data include JSON and XML are forms of semi-structured data. The reason that this third category exists (between structured and unstructured data) is because semi-structured data is considerably easier to analyse than unstructured data.

What is an example of a JSON struct? ›

Struct members representing a JSON object are addressable using the name, for example: vStruct->MemberName . Brackets ( [] denote a JSON array, which contains zero or more values separated by commas. A value can be an object or an array.

What is JSON and how does it work? ›

JSON is a data format that's commonly used by web developers for transferring data between a server and a web application. Developers often prefer JSON because it simplifies the exchange of data between different technologies.

What type of data can be stored in a JSON file? ›

In JSON, array values must be type string, number, object, array, Boolean, or null. Strings. In JSON, strings are enclosed in double quotation marks, can contain any Unicode character, and are commonly used to store and transmit text-based data, such as names, addresses, or descriptions.

What is the difference between JSON and XML? ›

As a markup language, XML is more complex and requires a tag structure. In contrast, JSON is a data format that extends from JavaScript. It does not use tags, which makes it more compact and easier to read for humans. JSON can represent the same data in a smaller file size for faster data transfer.

How to identify JSON data? ›

Proper JSON Format
  1. Data is in name/value pairs.
  2. Data is separated by commas.
  3. Objects are encapsulated within the opening and closing curly brackets.
  4. An empty object can be represented by {}
  5. Arrays are encapsulated within opening and closing square brackets.
  6. An empty array can be represented by []

What is the purpose of JSON? ›

JSON is used in electronic data exchange, such as transmitting data in web applications. Websites are made of web pages. These web pages display pre-stored information in a server and interact with the server using data formats such as JSON.

How do you declare and define a structure? ›

You begin a structure declaration with the Structure Statement, and you end it with the End Structure statement. Between these two statements you must declare at least one element. The elements can be of any data type, but at least one must be either a nonshared variable or a nonshared, noncustom event.

How a structure is defined? ›

A structure can be defined as a single entity holding variables of different data types that are logically related to each other. All the data members inside a structure are accessible to the functions defined outside the structure.

How do you define JSON structure in Python? ›

You can use the json. dumps() function provided by the JSON module to create JSON from a Python dictionary. This function takes a Python object, typically a dictionary, and converts it into a JSON string representation.

Top Articles
Latest Posts
Article information

Author: Tyson Zemlak

Last Updated:

Views: 5979

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.