Skip to content
the poor coder

dlowlad the json fiel in react js pretty formate

Poor Man
Published
Reading time
2 min read
On this page

How to Download and Format JSON files in React JS#

Introduction#

As a React JS developer, having the ability to format JSON files in a readable and easily understandable manner is essential. This is where the "pretty format" comes in. In this blog post, we will discuss how to download and format JSON files in React JS using the "pretty format" method.

Step 1: Download the JSON File#

The first step is to download the JSON file that you want to format. You can use various methods to download the file, such as using an API call or importing it from a local file. Once you have the file, you can proceed with formatting it.

Step 2: Install the Needed Dependencies#

In order to format the JSON file, we need to install two dependencies: "prettier" and "highlight.js". You can do this by running the following command in your terminal:

Text
npm install prettier highlight.js

Step 3: Create a Function to Format the JSON File#

The next step is to create a function that will format the JSON file. This function will use the "prettier" dependency to format the file with proper indentation and line breaks. Here is an example of such a function:

Text
function formatJson(json) {
  return JSON.stringify(JSON.parse(json), null, 4);
}

Step 4: Display the Formatted JSON File#

Finally, we need to display the formatted JSON file on our React JS application. We can use the "highlight.js" dependency to achieve this. Here is an example of how to display the formatted JSON file:

Text
{
  <div className="json">
    <pre>
      <code
        className="json"
        dangerouslySetInnerHTML={{
          __html: hljs.highlight(
            "json",
            formatJson(JSON.stringify(json, null, 2))
          ).value,
        }}
      />
    </pre>
  </div>
}

Alternative Method: React JSON Pretty#

Another way to format JSON files in React JS is to use the "react-json-pretty" library. This library allows you to format and display JSON files with ease. Here is an example of how to use it:

Text
import ReactJson from "react-json-pretty";

function JsonExample() {
  const json = {
    foo: "bar",
    nested: {
      hello: "world",
    },
  };

  return <ReactJson data={json} />;
}

Conclusion#

In conclusion, formatting JSON files in React JS is an essential skill that every developer should have. With the above methods, you can easily download and format JSON files in a readable and easily understandable manner. Remember to install the necessary dependencies, create a function to format the JSON file, and display it on your React JS application with the "highlight.js" dependency or the "react-json-pretty" library.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC