» npm install @uiw/react-json-view
» npm install react-json-view
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.
» npm install react-json-view-lite
» npm install react-json-view
I think that your issue is more related to the conversion from an object to an array, to be able to iterate on it. There are multiple ways to do that:
1 - You can use Object.entries() as @hgb123 recommended you:
const myObject = { a: 1, b: 2, c: 3};
const myArray = Object.entries(myObject);
console.log(myArray);
2 - You can get the keys of the object using Object.keys(), as @Gandzal recommended you, and iterate on the returned array:
const myObject = { a: 1, b: 2, c: 3};
const myKeys = Object.keys(myObject);
console.log(myKeys);
const myArray = myKeys.map(key => [key, myObject[key]]);
console.log(myArray);
3 - Or you can use Array.prototype.reduce() on the array returned by Object.keys():
const myObject = { a: 1, b: 2, c: 3};
const myArray = Object.keys(myObject).reduce((arr, key) => [...arr, [key, myObject[key]]], []);
console.log(myArray);
Whatever method you choose, iterate on an array, and display its results is easy after the conversion:
const data = {
"2020-09-19": [
{
end: "2020-09-19T10:30:00Z",
start: "2020-09-19T06:52:10Z",
user: "rakul",
},
{
end: "2020-09-19T18:30:00Z",
start: "2020-09-19T10:30:00Z",
user: "jeet",
},
{
end: "2020-09-20T02:30:00Z",
start: "2020-09-19T18:30:00Z",
user: "rahul",
},
],
"2020-09-22": [
{
end: "2020-09-20T10:30:00Z",
start: "2020-09-20T02:30:00Z",
user: "rakul",
},
{
end: "2020-09-20T18:30:00Z",
start: "2020-09-20T10:30:00Z",
user: "jeet",
},
{
end: "2020-09-21T02:30:00Z",
start: "2020-09-20T18:30:00Z",
user: "rahul",
},
],
};
const List = props => Object.entries(props.data).map(([date, items]) => (
<div key={date}>
<strong>{date}</strong><br/><br/>
{
items.map((content, index) => (
<span key={index}>
start: {content.start}<br/>
end: {content.end}<br/>
user: {content.user}<br/><br/>
</span>
))
}
</div>
));
ReactDOM.render(
<List data={data} />,
document.getElementById('example')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>
<div id="example"></div>
Use Object.entries()
const data = {
"2020-09-19": [
{
end: "2020-09-19T10:30:00Z",
start: "2020-09-19T06:52:10Z",
user: "rakul",
},
{
end: "2020-09-19T18:30:00Z",
start: "2020-09-19T10:30:00Z",
user: "jeet",
},
{
end: "2020-09-20T02:30:00Z",
start: "2020-09-19T18:30:00Z",
user: "rahul",
},
],
"2020-09-22": [
{
end: "2020-09-20T10:30:00Z",
start: "2020-09-20T02:30:00Z",
user: "rakul",
},
{
end: "2020-09-20T18:30:00Z",
start: "2020-09-20T10:30:00Z",
user: "jeet",
},
{
end: "2020-09-21T02:30:00Z",
start: "2020-09-20T18:30:00Z",
user: "rahul",
},
],
}
Object.entries(data).forEach(([date, content]) => {
console.log(date)
content.forEach((c) => {
console.log('\t', c.start, c.end, c.user)
})
})
» npm install @microlink/react-json-view
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>));
TLDR
Pretty Print JSON in JavaScript and React
<pre>{JSON.stringify(data, null, 2)}</pre>