Author avatar

Mayank Shukla

How to Display Key and Value Pairs from JSON in ReactJS

Mayank Shukla

  • Jan 26, 2020
  • 10 Min read
  • Jan 26, 2020
  • 10 Min read
Web Development


JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy to read and write and for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition.

JSON is basically a collection of name/value pairs, where the name will always be a string and values can be a string (in double quotes), a number, a boolean (true or false), null, an object, or an array. Each name-value pair will be separated by a comma. JSON is a text format that is completely language independent, and it is an ideal way of exchanging data.

Before jumping in, let's discuss React, a popular JavaScript library for building user interfaces. It basically handles the view-layer of the application. The best part is that it allows you to break a component into smaller, reusable components. Instead of putting all the logic into a single file, writing smaller components gives us a better edge. By writing well-encapsulated components, we are essentially making a testable app with a good separation of concerns and maximum code reuse.

Rules on Name Strings

JSON syntax does not impose any restrictions on the strings used as name, does not require that name strings be unique, and does not assign any significance to the ordering of name/value pairs. These are all semantic considerations that may be defined by JSON processors or in specifications defining specific uses of JSON for data interchange.

Possible Values of Name

A JSON value can be an object, array, number, string, true, false, or null, and JSON structure can be nested up to any level. So for the sake of simplicity and to understand the rendering part easily, let's assume a JSON name can have two types of values, a leaf value or a nested structure. A leaf value can be string, number, true, false, or null. For a nested structure, value will be an array (ordered list) or an object (unordered set of name-value pairs).

Objects and arrays are useful for representing and sharing complex nested data.


2  "string": "Pluralsight",
3  "number": 1,
4  "true": true,
5  "false": false,
6  "null": null,
7  "arrayOfStrings": ["a", "b", "c", "d"],
8  "arrayOfNumbers": [1, 2, 3, 4, 5],
9  "arrayOfBooleans": [true, false, true, false],
10  "arrayOfObjects": [{ "a": 1 }, { "a": 2}],
11  "object": {
12    "anyKey": "anyValue"
13  }

How to Render Dynamic Value in JSX

Before beginning the discussion on rendering name-value pair, lets cover the rendering of dynamic values in React and what exactly JSX is.

We do not necessarily use JSX with React, but it is very helpful when working with UI. With React, we don't write HTML for UI creation, we write JSX. JSX is basically syntax extension to JavaScript. Nothing but syntactic sugar for React.createElement(component, props, ...children).

For example, these two lines are same. The second line is transpiled version of the first.

1<div>Hello World!!!</div> // jsx
2React.createElement('div', null, 'Hello World!!!');

To render dynamic values in JSX, we use {} (curly braces). Code that we put in {} will be treated as JS expression and will be evaluated at runtime (the expression must be a valid JS expression).

Rendering JSON Values

There can be many sources of JSON files, either through an API call or importing a locally created JSON file (with extension .json). In most front-end applications, we interact with a server to fetch the data and fill the UI skeleton or template. For now, I am assuming JSON is available in component. One by one, we will discuss all the values and how to render or show them in UI.

Rendering String/Number Values

A string is a sequence of zero or more Unicode characters wrapped in double quotes. String or number values can be rendered directly in JSX using {}. For example:

1import React from 'react';
3const sampleJSON = {
4  "string": "PluralSight",
5  "number": 1
8function App() {
9  return(
10    <div>
11      <p>String : {sampleJSON.string}</p>
12      <p>Number : {sampleJSON.number}</p>
13    </div>
14  )
17ReactDOM.render(<App />, document.getElementById('app'));

Rendering Null/True/False

Boolean values (true and false), null, and undefined are valid children, but these values will not be rendered in UI if you put them directly inside {} in JSX. For example, all these JSX expressions will result in the same empty div:

1<div />

If you want a value like false, true, null, or undefined to show in the output, you have to convert it to a string first.

For example:


In the output, this will render true, false, undefined, and null respectively.

Note: There are many possible ways to convert these values into a string. Use any one.

Rendering Objects

Here comes the most tricky part of the JSON name-value pair rendering. React does not directly allow us to render any object in JSX. If we mistakenly do that, it will result in the following error:

Objects are not valid as a React child (found: object with keys { a, b, c }). If you meant to render a collection of children, use an array instead.

Now the question is how to render an object without breaking the code?

As we have already discussed, we can only render leaf values. In the case of an object, we have to specify the exact property name using either dot notation or bracket notation, which has the renderable value.

For example, to render all the properties of the object in the UI, we need to run a loop:

1import React from 'react';
2import ReactDOM from 'react-dom';
4const sampleJSON = {
5  "object": {
6    "name": "Pluralsight",
7    "number": 1,
8    "address": "India",
9    "website": ""
10  }
13function App() {
15  return (
16    <div>
17      {
18        Object.keys(sampleJSON.object).map((key, i) => (
19          <p key={i}>
20            <span>Key Name: {key}</span>
21            <span>Value: {sampleJSON.object[key]}</span>
22          </p>
23        )
24      }
25    </div>
26  )
29ReactDOM.render(<App />, document.getElementById('app'));

Note: If we want to show only one renderable property in UI, then we don't need to run a loop. We can simply write it using dot notation. For example, to display the name value of the object:

1<p>Name: {}</p>

Rendering Array Values

And now, the last part of the guide, but of course not the least. Rendering array values in UI with proper styling is one of the common things we do in applications such as Todo App, Travel App, etc.

First let's discuss a case where we want to render an array of leaf values without any extra styling or HTML tag for each item. This will be similar to joining the values using a comma or space, then rendering that string. Array (with all renderable value) can be rendered directly like this:

1import React from 'react';
2import ReactDOM from 'react-dom';
4const sampleJSON = {
5  "arrOfNumbers": [1, 2, 3, 4],
6  "arrOfStrings": ["a", "b", "c", "d"]
9function App() {
10  return (
11    <div>
12      {sampleJSON.arrOfNumbers}
13      {sampleJSON.arrOfStrings}
14    </div>
15  )
18ReactDOM.render(<App />, document.getElementById('app'));

Next, either we want separate styling for each item or the item is not a directly renderable value. In all those cases, we need to run a loop. Luckily, this is easy to do in JSX using When we use map, it will run through all the items in that array and we can return a new array of JSX.

Consider this example:

1import React from 'react';
2import ReactDOM from 'react-dom';
4const sampleJSON = {
5  "arrOfNumbers": [1, 2, 3, 4],
6  "arrOfStrings": ["a", "b", "c", "d"],
7  "arrOfObjects": [{ "a": 1, "b": 1 }, { "a": 2, "b": 2 }, { "a": 3, "b": 3 }]
10function App() {
11  return (
12    <div>
14      <h2>Array of Numbers:</h2>
15      <ul>
16        {, i) => {
17          return <li key={i}>{item}</li>
18        })}
19      </ul>
21      <h2>Array of Strings:</h2>
22      <ul>
23        {, i) => {
24          return <li key={i}>{item}</li>
25        })}
26      </ul>
28      <h2>Array of Objects:</h2>
29      <ul>
30        {, i) => {
31          return <li key={i}>{item.a} - {item.b}</li>
32        })}
33      </ul>
35    </div>
36  )
39ReactDOM.render(<App />, document.getElementById('app'));


I hope this guide was helpful in understanding the basics of JSON, React, JSX, and how to render different types of values efficiently to create meaningful user interfaces.

Learn More

Explore these JSON and React courses from Pluralsight to continue learning: