You'll need to either insert BR tag appropriately in the resulting string, or use for example a PRE tag so that the formatting of the stringify is retained:
var data = { a: 1, b: 2 };
var Hello = React.createClass({
render: function() {
return <div><pre>{JSON.stringify(data, null, 2) }</pre></div>;
}
});
React.render(<Hello />, document.getElementById('container'));
Working example.
Update
class PrettyPrintJson extends React.Component {
render() {
// data could be a prop for example
// const { data } = this.props;
return (<div><pre>{JSON.stringify(data, null, 2) }</pre></div>);
}
}
ReactDOM.render(<PrettyPrintJson/>, document.getElementById('container'));

Stateless Functional component, React .14 or higher
const PrettyPrintJson = ({data}) => {
// (destructured) data could be a prop for example
return (<div><pre>{ JSON.stringify(data, null, 2) }</pre></div>);
}
Or, ...
const PrettyPrintJson = ({data}) => (<div><pre>{
JSON.stringify(data, null, 2) }</pre></div>);
Working example
Memo / 16.6+
(You might even want to use a memo, 16.6+)
const PrettyPrintJson = React.memo(({data}) => (<div><pre>{
JSON.stringify(data, null, 2) }</pre></div>));
Answer from WiredPrairie on Stack Overflownpm
npmjs.com › package › react-json-pretty
react-json-pretty - npm
A code formatting tool for raw json data. Latest version: 2.2.0, last published: 6 years ago. Start using react-json-pretty in your project by running `npm i react-json-pretty`. There are 124 other projects in the npm registry using ...
» npm install react-json-pretty
Published Oct 14, 2019
Version 2.2.0
Author chenckang@gmail.com
Top answer 1 of 7
375
You'll need to either insert BR tag appropriately in the resulting string, or use for example a PRE tag so that the formatting of the stringify is retained:
var data = { a: 1, b: 2 };
var Hello = React.createClass({
render: function() {
return <div><pre>{JSON.stringify(data, null, 2) }</pre></div>;
}
});
React.render(<Hello />, document.getElementById('container'));
Working example.
Update
class PrettyPrintJson extends React.Component {
render() {
// data could be a prop for example
// const { data } = this.props;
return (<div><pre>{JSON.stringify(data, null, 2) }</pre></div>);
}
}
ReactDOM.render(<PrettyPrintJson/>, document.getElementById('container'));

Stateless Functional component, React .14 or higher
const PrettyPrintJson = ({data}) => {
// (destructured) data could be a prop for example
return (<div><pre>{ JSON.stringify(data, null, 2) }</pre></div>);
}
Or, ...
const PrettyPrintJson = ({data}) => (<div><pre>{
JSON.stringify(data, null, 2) }</pre></div>);
Working example
Memo / 16.6+
(You might even want to use a memo, 16.6+)
const PrettyPrintJson = React.memo(({data}) => (<div><pre>{
JSON.stringify(data, null, 2) }</pre></div>));
2 of 7
64
TLDR
Pretty Print JSON in JavaScript and React
<pre>{JSON.stringify(data, null, 2)}</pre>
Videos
05:58
How to Render JSON in React | Custom Pretty-Print Component Tutorial ...
How to Pretty Print JSON | React Tutorial
00:11
Pretty Print JSON with React #shorts #react - YouTube
01:12
How to Pretty Print JSON | React Tutorial - YouTube
09:26
Working with JSON data in React.js | Part 27 | Introduction To ...
09:14
2 - Display JSON data on a Map using React Leaflet - React Leaflet ...
CodeSandbox
codesandbox.io › examples › package › react-json-pretty
react-json-pretty examples - CodeSandbox
Use this online react-json-pretty playground to view and fork react-json-pretty example apps and templates on CodeSandbox.
GitHub
github.com › GuillaumeCisco › react-json-prettify
GitHub - GuillaumeCisco/react-json-prettify: Simple and Lightweight React Component for displaying Json
import JSONPretty from 'react-json-prettify'; import {github} from 'react-json-prettify/themes'; const json = { name: 'John Doe', age: 20, admin: true, member: null, permissions: ['read', 'write', 'edit'], deep: [ { a: { b: { c: null, d: ['e', 'f', [1, null]], }, }, }, ], }; return <JSONPretty json={json} theme={github} padding={4} />
Author GuillaumeCisco
GitHub
github.com › chenckang › react-json-pretty
GitHub - chenckang/react-json-pretty: A react component for prettifying JSON in browser
Starred by 160 users
Forked by 24 users
Languages JavaScript 98.2% | TypeScript 1.6%
npm
npmjs.com › package › react-json-formatter
react-json-formatter - npm
import React from 'react' import JsonFormatter from 'react-json-formatter' const App = () => { const sample = `{ "string":"ABCDE", "number":1, "null":null, "boolean":true, "object":{ "string":"ABCDE", "number":1, "null":null, "boolean":true }, "array":[ 1, 2, 3, 4, { "string":"ABCDE", "number":1, "null":null, "boolean":true, "array":[ 1, 2, 3, 4, { "string":"ABCDE", "number":1, "null":null, "boolean":true } ] } ] } ` const jsonStyle = { propertyStyle: { color: 'red' }, stringStyle: { color: 'green' }, numberStyle: { color: 'darkorange' } } return <JsonFormatter json={sample} tabWith={4} jsonStyle={jsonStyle} /> } export default App
» npm install react-json-formatter
Published Jul 04, 2023
Version 0.4.0
Author ronny1020
npm
npmjs.com › package › react-json-prettify
react-json-prettify - npm
Simple and Lightweight React Component for displaying Json. Latest version: 0.2.0, last published: 6 years ago. Start using react-json-prettify in your project by running `npm i react-json-prettify`. There are no other projects in the npm registry ...
» npm install react-json-prettify
Published Feb 17, 2020
Version 0.2.0
Author Guillaume Cisco
GitHub
github.com › tanmancan › react-json-print
GitHub - tanmancan/react-json-print: React component that pretty prints JSON object in a collapsible tree view
React component that pretty prints JSON object in a collapsible tree view - tanmancan/react-json-print
Starred by 7 users
Forked by 2 users
Languages JavaScript 76.4% | TypeScript 20.0% | HTML 2.1% | CSS 1.5%
Techboxweb
techboxweb.com › how-to-pretty-print-json-in-react
How to pretty print JSON in react? - TechBoxWeb
Next Js is a very powerful react framework that lets you render React on the server side. Next Js comes with a lot… · September 5, 2020Comments Off on How to pretty print JSON in react?
GitHub
github.com › loopmode › react-pretty-json
GitHub - loopmode/react-pretty-json: A react component that pretty-prints JSON data, based on http://jsfiddle.net/unlsj/
A react component that pretty-prints JSON data, based on http://jsfiddle.net/unlsj/ - loopmode/react-pretty-json
Author loopmode
GitHub
github.com › fis-components › react-json-pretty
GitHub - fis-components/react-json-pretty: Fork from https://github.com/chenckang/react-json-pretty.git
This is a react component that help you to prettify your json strings on the browser based on JavaScript.
Author fis-components
Marinus Klasen
mklasen.com › home › blog › general › dump / pretty print a json object in react
Dump / pretty print a JSON object in React - Marinus Klasen
July 28, 2022 - To quickly debug something, or temporarily show the data object during development, just to know what you’re working with, it comes in handy to print the full JSON object. Not as a string that’s almost impossible to read, but as a well formatted array. We’ve got a neat little one-liner for that, which I’ll share […]
Maxheinritz
maxheinritz.com › posts › react-json-pretty-print.html
React JSON pretty print
import React from "react"; function SimpleJsonPrettyPrint(props) { const { jsonObject } = props; return <pre>{JSON.stringify(jsonObject, undefined, 2)}</pre>; }