Function json_vfprintf convert {"three":2,"two":2,"one":2} to {""three"":1,""two"":1,""one"":1}

Hello

I’m working on queuing MQTT messages in the absence of internet and I ran into a problem writing a file to disk, my input is
{“three”:2,“two”:2,“one”:2}

when I use function json_vfprintf

the data stored on the disk looks like this
{"“three”":1,"“two”":1,"“one”":1}
which is incompatible with JSON

Is it possible to call the function so that it doesn’t add another " to the JSON or in somehow delete extra " from file ?

Or it’s possible to convert mqtt_message in mjs to {three:2,two:2,one:2} not to {“three”:2,“two”:2,“one”:2} ??

let mqtt_message = {};
mqtt_message.one = i;
mqtt_message.two = i;
mqtt_message.three = i;

You don’t need to do that, when you “send” a message it is in fact queued and sent later

What are you doing, exactly (code) ? If you call a JSON function, pass it JSON, not stringified JSON, that is, the quotation marks are not JSON…

I’m making an IIoT device and I want MQTT packets to be cached for a few days, I’ve seen caching in the MQTT library but it only caches a few packets.

I modified the library

I added support for mJS but I have a problem with converting to JSON, the library sends the packages correctly but queue file is written in the form {“”three”“:1,”“two”“:1,”“one”“:1} and no i know how to solve this problem.

This function
int result = json_vfprintf((const char*) new_file, json_fmt, ap);

convert {“three”:2,”two”:2,”one”:2} to {“”three”“:1,”“two”“:1,”“one”“:1}

As @scaprile said, show some example code you use.

I use this library

from mJS I call

Timer.set(10000 /* milliseconds /, true / repeat */, function () {
let mqtt_message = {};
mqtt_message.one = i;
mqtt_message.two = i;
mqtt_message.three = i;
let s = mkstr(mqtt_message, 20);
print(‘STRING ’ + s);
let message = JSON.stringify(mqtt_message);
let topic = ‘test/99/’ + JSON.stringify(i);
print(’== Publishing to ’ + topic + ‘:’, message);
MQTT_queue.pub(topic, message);
i++;
}, null);

Ok. I see what happens. You try to ffi a variadic function.

I don’t know if there is a trick to do that. You should create a function bool mgos_mqtt_queue_gcp_send_event_sub_json(const char *subfolder, const char *json) which takes a json string and ffi it, or simply call the existing function from C code.

But it’s work I only have problem with this function:
int result = json_vfprintf((const char*) new_file, json_fmt, ap);

It’s convert {“three”:2,”two”:2,”one”:2} to {“”three”“:1,”“two”“:1,”“one”“:1}

Is there any other way to save the file to disk so as not to add more quotes.

Or convert mqtt_message object to string with I can send using MQTT_queue.pub(topic, message) but without quotas (not using JSON.stringify(mqtt_message):wink: ?

let mqtt_message = {};
mqtt_message.one = i;
mqtt_message.two = i;
mqtt_message.three = i;
let s = mkstr(mqtt_message, 20);
print(‘STRING ’ + s);
let message = JSON.stringify(mqtt_message);

json_vfprintf works as it is intended to. If you provide it with a json string, it will add another pair of qoutes to the json key.
The error comes from your code.
Try to do what I suggested in my previous message.

Thanks for the tip but I don’t know what to do.

There are no functions in C code
mgos_mqtt_queue_gcp_send_event_sub_json(const char *subfolder, const char *json)

I have instead

ffi('bool mgos_mqtt_queue_gcp_send_event_subf(char *, void *)'),

which points to

bool mgos_mqtt_queue_gcp_send_event_subf(const char *subfolder, const char *json_fmt, …) {
res = false;
va_list ap;
va_start(ap, json_fmt);

if ( ! mgos_mqtt_global_is_connected() ){
LOG(LL_DEBUG, ("%s", “MQTT QUEUE NOT CONNECTED, QUEUE FILE”));
LOG(LL_DEBUG, ("%s", json_fmt));
LOG(LL_DEBUG, ("%s", subfolder));
add_to_queue(json_fmt, ap, subfolder );
} else {

 char *data = json_vasprintf(json_fmt, ap);
 if (date != NULL) {
     res = mgos_mqtt_pub(subfolder, json_fmt, strlen(json_fmt), 1, false);
     //res = mgos_mqtt_pubf(subfolder, 1 /* qos */, false /* retain */, json_fmt);
     free(date);
 }

}
va_end(ap);
return res;
}

What should I do ?

Ok I handle with it.

I edited library and now it’s working and saving unsend message to file.

Library is here, I must check sending queue massage from file when device recive Internet, it save massage to file but I must restart device to send queue.