Creating nested JSON string by passing JSONBufferWriter object to a function

I created a JSONBufferWriter object {“a”:123,“b”:123} for example. I want to pass it to a function and nest it into another JSON object like {“result”:{“a”:123,“b”:123}}.
I can’t pass this as a string because it will escape the double quotes. So I want something like this:

void responseFormatJSON(JSONBufferWriter *resp) {
    // generate JSON response
    JSONBufferWriter writer(response, sizeof(response)-1);
    writer.beginObject();
        writer.name("result").beginObject()
            .value(resp)
            .endObject();
    writer.endObject();
    // add null terminator
    writer.buffer()[std::min(writer.bufferSize(), writer.dataSize())] = 0;
}

Except this results in {“response”:{true}}
How do I get the JSON format I need?

You normally flip it around the other way. The outer code does this part:

JSONBufferWriter writer(response, sizeof(response)-1);
    writer.beginObject();
        writer.name("result")

Then it calls the other function, passing the &writer to it, the JSONBufferWriter *.

The other function that adds its object, a, and b, by doing:

writer->beginObject()
  .name("a").value(a)
  .name("b").value(b)
  .endObject();

And returns.

The outer code then finishes with:

 writer.endObject();
    // add null terminator
    writer.buffer()[std::min(writer.bufferSize(), writer.dataSize())] = 0;

@rickkas7 If I understand your response you suggest I have to create the outer JSON first and then create the inner JSON object. But in my case I don’t think this will work.

I create the inner JSON object as my data. It has 10-12 key/value pairs which can come from a few different functions. Then I need to add a wrapper around the data as a second operation because it is more general.

I want the inner JSON to be an object, not a string. The string has escaped double quotes which adds length and also reduces ease of parsing.

Did I not understand your response correctly?

It’s usually better to create the outside object first, because then you only need one buffer, and fewer copies, but if that structure won’t work, I added a new method to insert pre-formatted JSON into an existing JsonWriter object:

	/**
	 * @brief Inserts a new key and an existing valid JSON object or array in a c-string
	 *
	 * @param key the key name to insert
	 * 
	 * @param json The object or array to insert, a c-string. Must already be valid JSON.
	 */
	void insertKeyJson(const char *key, const char *json);

There’s also insertJson if you want to insert pre-formatted JSON in an array.

	/**
	 * @brief Used to insert a string of existing JSON (typically a preformatted object or array) into a writer
	 *
	 * @param json The object or array to insert, a c-string. Must already be valid JSON.
	 */
	void insertJson(const char *json);

0.1.5 (2021-08-18)

  • Added JsonWriter::insertKeyJson so you can insert a pre-formatted JSON object into an existing JsonWriter.

You are excellent!
I’ll try the new library from https://github.com/rickkas7/JsonParserGeneratorRK

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.