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
» npm install react-json-pretty
Published Oct 14, 2019
Version 2.2.0
Author chenckang@gmail.com
Videos
How to Pretty Print JSON | React Tutorial
00:11
Pretty Print JSON with React #shorts #react - YouTube
05:58
How to Render JSON in React | Custom Pretty-Print Component Tutorial ...
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 ...
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
import React, { Component } from 'react'; import PrettyJson from '@loopmode/react-pretty-json'; export class App extends Component { state = { foo: "foo", bar: "bar", nested: { stuff: "we needed a nested value", some: Immutable.fromJS({ data: 'immutable as well!'
Author loopmode
GitHub
github.com › tanmancan › react-json-print
GitHub - tanmancan/react-json-print: React component that pretty prints JSON object in a collapsible tree view
Pretty prints JSON object in a collapsible tree view. A Vue.js verion is available here · Demo: https://tanmancan.github.io/examples/react-json-print/
Starred by 7 users
Forked by 2 users
Languages JavaScript 76.4% | TypeScript 20.0% | HTML 2.1% | CSS 1.5% | JavaScript 76.4% | TypeScript 20.0% | HTML 2.1% | CSS 1.5%
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% | JavaScript 98.2% | TypeScript 1.6%
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.
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
More css customization (background-color, background-url, font-weight). Styles of brackets and braces. Inline options for arrays. react · json · pretty · prettify · nice · beautiful · awesome · npm i react-json-prettify · github.com/GuillaumeCisco/react-json-prettify ·
» npm install react-json-prettify
Published Feb 17, 2020
Version 0.2.0
Author Guillaume Cisco
YouTube
youtube.com › watch
How to Pretty Print JSON | React Tutorial - YouTube
How to Pretty Print JSONFree crash courses:React JWT Authentication: https://youtu.be/T5dIjye4b-IReact Redux: https://youtu.be/RPFr-hiFiU0React Redux Saga: h...
Published July 29, 2023
Maxheinritz
maxheinritz.com › posts › react-json-pretty-print.html
React JSON pretty print
"" : ","; if (customValueRenderer) { const renderedValue = customValueRenderer(value); if (renderedValue) { return ( <> {renderedValue} {maybeTrailingComma} </> ); } } return ( <> <JsonPrettyPrintValue value={value} customValueRenderer={customValueRenderer} indent={indent} depth={depth} maxDepth={maxDepth} /> {maybeTrailingComma} </> ); } function JsonPrettyPrintValue({ value, customValueRenderer, indent, depth, maxDepth, }: { value: Record<string, any>; customValueRenderer?: (value: any) => JSX.Element | undefined; indent: number; depth: number; maxDepth: number; }) { if (value === undefined
CopyProgramming
copyprogramming.com › howto › how-do-i-display-prettyprinted-json-in-a-react-component
How to Display Pretty-Printed JSON in React Components: Complete Guide for 2026
December 27, 2025 - React provides both built-in JavaScript ... and most straightforward method to display pretty-printed JSON in React is using the native JavaScript JSON.stringify() method combined with the HTML <pre> tag....