Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json output into single line

Tags:

c++

json

boost

Question:

With below sample code, able to create a json string and print as below, my requirements is to print into a single line(below as well) with no space or tab, can i do it using boost or need to explicitly remove from string.

#include "boost/property_tree/json_parser.hpp"
#include <boost/property_tree/ptree.hpp>
#include <sstream>
#include <iostream>

    int main()
    {
            boost::property_tree::ptree root;
            root.put("sessionId", "123456");
            root.put("applicationName", "Sample_Appliation");
            root.put("applicationId", "null");
            root.put("logMessage", "Sample Message");
            root.put("loggingTime", "2020-03-17T13:26:45.013");
            std::stringstream ss;
            boost::property_tree::write_json(ss, root);
            std::cout << ss.str() << std::endl;
            return 0;
    }

Output:

{
    "sessionId": "123456",
    "applicationName": "Sample_Appliation",
    "applicationId": "null",
    "logMessage": "Sample Message",
    "loggingTime": "2020-03-17T13:26:45.013"
}

Expected:

{"sessionId": "123456","applicationName": "Sample_Appliation","applicationId": "null","logMessage": "Sample Message","loggingTime": "2020-03-17T13:26:45.013"}
like image 867
ashish Avatar asked Oct 21 '25 20:10

ashish


1 Answers

There is a third parameter to boost::property_tree::write_json named pretty with default value true, just pass false as a third parameter to disable pretty printing.

like image 174
kdcode Avatar answered Oct 24 '25 09:10

kdcode