0

So I want to turn a string into a JSON object but I'm not sure how. I'm using the Nlohmann library.

This is my string called "readBuffer":

{"sscresponse":{"errorcode":0, "errordesc":"-","data":{"pac_power_wr_kwh":"6.374","pac_power_all_wr_kwh":"6.622","voltage_ac_volt":"59","voltage_dc_volt":"673","daily_output_kwh":"6.904","yesterday_output_kwh":"0","monthly_output_kwh":"6.904","yearly_output_kwh":"25883.4","total_output_kwh":"296533","current_consumption_kwh":"0","daily_consumption_kwh":"0","yesterday_consumption_kwh":"0","monthly_consumption_kwh":"0","yearly_consumption_kwh":"0","sum_of_all_consumers_kwh":"0","installed_generator_power_kwp":"100"}}}

I've tried it like this:

json j2 = R"(
        {
            "happy": true,
            "pi": 3.141
        }
    )"_json;

    j2 = readBuffer;

    std::string s = j2.dump();

    std::cout << j2.dump(15) << std::endl;

This is the OUTPUT:

"{\"sscresponse\":{\"errorcode\":0, \"errordesc\":\"-\",\"data\":{\"pac_power_wr_kwh\":\"6.374\",\"pac_power_all_wr_kwh\":\"6.622\",\"voltage_ac_volt\":\"59\",\"voltage_dc_volt\":\"673\",\"daily_output_kwh\":\"6.904\",\"yesterday_output_kwh\":\"0\",\"monthly_output_kwh\":\"6.904\",\"yearly_output_kwh\":\"25883.4\",\"total_output_kwh\":\"296533\",\"current_consumption_kwh\":\"0\",\"daily_consumption_kwh\":\"0\",\"yesterday_consumption_kwh\":\"0\",\"monthly_consumption_kwh\":\"0\",\"yearly_consumption_kwh\":\"0\",\"sum_of_all_consumers_kwh\":\"0\",\"installed_generator_power_kwp\":\"100\"}}}"

I want it to be displayed like a normal JSON object:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

Could somebody help?

JustADude
  • 1
  • 2
  • 1
    *I want it to be displayed like a normal JSON object.* What do you mean? Your output looks very much like a normal JSON object to me. Are you looking for prettier formating? – super Oct 11 '21 at 08:28
  • Your input string is already JSON. By running it through the JSON processor again you've double-encoded it. It's not clear exactly what output you were expecting instead, though? Are you actually wanting to decode the JSON into a usable variable in your program? – ADyson Oct 11 '21 at 08:31
  • 1
    You need some JSON parsing library, – Victor Gubin Oct 11 '21 at 09:18
  • I want to have it in a JSON Format if I say it right. Google JSON Example and click on the first link, you'll see what I mean. – JustADude Oct 11 '21 at 14:01
  • 1
    But your input data is _already_ in JSON format, so it's not clear what you need instead. Be specific, don't just tell us to go and Google it. – ADyson Oct 11 '21 at 14:40
  • I've given an example in the question. – JustADude Oct 12 '21 at 06:06
  • Ok. So what you've shown is just pretty-printed JSON. It's not "normal" JSON (there's no such thing) and it's not necessary either - because JSON is primarily intended to be machine readable and machines don't care about presentation. You only see this type of formatting usually when the JSON is to be read by humans - e.g. in tutorials etc usually. But some JSON code libraries do contain the option to pretty print the output for convenience. If not, many text editors can do it via a plugin. In your case, take care not to double encode the data in the process. – ADyson Oct 12 '21 at 06:18
  • The reason I want to prettify it is because it'll be read by humans. Otherwise I wouldn't go this far. Anyway thank you, that does answer my question. – JustADude Oct 12 '21 at 07:18
  • That's a perfectly reasonable reason, then. It just wasn't clear what you meant originally. Glad it helps – ADyson Oct 12 '21 at 07:34

0 Answers0