cplusplus
cplusplus

 

 

Introduction

In programming, handling JSON data is common when working with APIs or data storage systems. However, JSON data can sometimes be difficult to read due to its compact format. This problem can be solved by formatting (pretty printing) the JSON data to be more human-readable.

The objective of this program is to take a JSON string and format it in a more readable structure using the C++ programming language. This includes adding appropriate indentations, line breaks, and other formatting techniques to improve clarity.

Objective

The goal is to parse the JSON string and output it in a more readable format with proper indentation and line breaks. This approach will help when debugging or when presenting the JSON data to end-users in an application.

Code

#include 
#include 
#include <nlohmann/json.hpp> // Include the JSON library

using json = nlohmann::json;

// Function to format the JSON string
std::string formatJson(const std::string& jsonString) {
    // Parse the input JSON string
    json parsedJson = json::parse(jsonString);
    
    // Format the JSON with indentation
    return parsedJson.dump(4); // 4 spaces for indentation
}

int main() {
    // Input JSON string
    std::string jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\",\"children\":[{\"name\":\"Anna\",\"age\":10},{\"name\":\"Ella\",\"age\":8}]}";
    
    // Format the JSON and print the result
    std::string formattedJson = formatJson(jsonString);
    std::cout << "Formatted JSON: \n" << formattedJson << std::endl;
    
    return 0;
}

Explanation

In this program, we use the nlohmann::json library, which simplifies working with JSON in C++. The program follows these key steps:

  • The JSON string is parsed using json::parse().
  • The dump() method is used to format the JSON with a specified number of spaces (in this case, 4 spaces for indentation).
  • The formatted JSON string is printed to the console for better readability.

How to Run the Program

To run this program, you need to have the nlohmann/json library installed. Follow these steps:

  1. Download and install the nlohmann/json library.
  2. Compile the C++ code using a C++ compiler (e.g., g++ filename.cpp -o program).
  3. Run the compiled program by executing ./program on your terminal.

Conclusion

By using the nlohmann/json library, we can easily format JSON strings in C++ for improved readability. This is useful for debugging, testing, and displaying JSON data in a more structured way.

© Learn Programming

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)