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 Overflow 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>
npm
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
Videos
05:58
How to Render JSON in React | Custom Pretty-Print Component 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 › loopmode › react-pretty-json
GitHub - loopmode/react-pretty-json: A react component that pretty-prints JSON data, based on http://jsfiddle.net/unlsj/ · GitHub
A react component that pretty-prints JSON data, based on http://jsfiddle.net/unlsj/ - loopmode/react-pretty-json
Author loopmode
Reddit
reddit.com › r/reactjs › which json viewer component do you recommend since react-json-view no one maintains it anymore.
r/reactjs on Reddit: Which JSON Viewer Component do you recommend since react-json-view no one maintains it anymore.
October 16, 2023 -
https://www.npmjs.com/package/react-json-view?activeTab=versions
It was last updated 3 years ago.
I am looking for a popular alternative to that.
Thanks.
DEV Community
dev.to › gauravadhikari1997 › show-json-as-pretty-print-with-syntax-highlighting-3jpm
Show JSON As Pretty Print With Syntax Highlighting in React - DEV Community
June 24, 2025 - Now we can use our syntaxHighlight function, just pass it with JSON to prettify and highlight ... import { useEffect, useState } from "react"; import { syntaxHighlight } from "./utils"; import "./styles.css"; export default function App() { const [ourJSON, setOurJSON] = useState(); useEffect(() => { fetch("https://jsonplaceholder.typicode.com/todos/1") .then((response) => response.json()) .then((json) => setOurJSON(json)); }, []); return ( <div> <h3 className="header"> Show JSON As Pretty Print With Syntax Highlighting </h3> <pre dangerouslySetInnerHTML={{ __html: syntaxHighlight(JSON.stringify(ourJSON, undefined, 4)) }} /> </div> ); }
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'; 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} /> Output will look like ·
Author GuillaumeCisco
npm
npmjs.com › package › react-json-formatter
react-json-formatter - npm
July 4, 2023 - 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
CodeSandbox
codesandbox.io › s › react-json-prettify-demo-cjqz4
react-json-prettify demo - CodeSandbox
January 29, 2020 - react-json-prettify demo by GuillaumeCisco using react, react-dom, react-json-prettify, react-scripts
GitHub
github.com › chenckang › react-json-pretty › issues
chenckang/react-json-pretty
A react component for prettifying JSON in browser. Contribute to chenckang/react-json-pretty development by creating an account on GitHub.
Author chenckang
jsDelivr
jsdelivr.com › package › npm › react-json-pretty
react-json-pretty CDN by jsDelivr - A CDN for npm and GitHub
October 14, 2019 - A free, fast, and reliable CDN for react-json-pretty. A code formatting tool for raw json data
Published Dec 29, 2015