Transform a JSON string to a "C/C++" sprintf formatting string for simple JSON serialization. Runs online in Google Colab.
If you sometimes have a need or desire to generate low overhead JSON strings for an Arduino or other "C/C++" project, JSON2C can help. Creating JSON strings by hand is a painstaking, time consuming, tedious activity requiring lots of time in the edit->compile->test->debug development loop. JSON librarys certainly help, but also can add considrable to you code and frequently provide more capability than actually needed.
This tool presumes that you only have a need to generated serialized JSON data, and do not have a need to decode JSON data strings.
Enter or paste your JSON string into the js
variable, and then click the run
icon to the left.
That will load your JSON into the js
variable.
Once you have the js
loaded, click the run
button in the next cell to convert your JSON to a compilable 'C' sprintf or printf formatting string in Arduino or other "C" project.
JASONata or JSON Editor Online or other JSON tool to build, verify, and test your JSON string. Simply use "C/C++" formatting specifiers in the place of the JSON values, and JSON2C will generate the "C/C++" formatting string for you.
Note that you can enter a "C" comment by using the "comment_" JSON key and placing your comment text as the value string. You can name your "C/C++" format variable with the JSON key "format_name_" with the desired "C" name in the value string. See examples below. I enclosed my JSON in triple quotes ''' but it should work ok with single quotes as well.
This example shows the js
variable loaded with some JSON. You can compile and run various example "C" programs wth the JSON2C_examples.ipynb notebook. You can access it by clicking here:
js = '''
{
"format_name_":"my_name",
"comment_": " This is a comment. It can contain numbers and any of the following chars {}[],.;?<>| %^&*()@#$ : %!",
"myJSON": {
"dest": "%s",
"sss": "%0.2f",
"flash": [
"%f",
"%4.2lf",
"%ld",
"%lu",
"%u",
"%d",
"%c"
]
}
}
'''
Running JSON2C on the above produces the following output which you can copy and paste into your Arduino or other "C" language source code.
The top block of code is simply the JSON string you entered. The lower block of code is the generated sprintf formatting string.
Note that the "comment_"
entry is removed from the formoatting string as is the "format_name_"
string. The "format_name_"
value
contains the "C" variable name you want to assign to this formatting string. The comment is simply so you can optionally provide
some documentation to the code.
/* This is a comment. It can contain numbers and any of the following chars {}[],.;?<>| %^&*()@#$ : %!
{
"format_name_":"my_name",
"comment_": " This is a comment. It can contain numbers and any of the following chars {}[],.;?<>| %^&*()@#$ : %!",
"myJSON": {
"dest": "%s",
"sss": "%-19.2f",
"flash": [
"%f",
"%4.2lf",
"%ld",
"%lu",
"%u",
"%d",
"%c"
]
}
}
*/
/* This was autogenerated from the above JSON using js2c.ipynb */
const char my_name[] = ""
""
"{ "
"\"myJSON\": {"
" \"dest\": \"%s\","
" \"sss\": \"%-19.2f\","
" \"flash\": ["
" %f,"
" %4.2lf,"
" %ld,"
" %lu,"
" %u,"
" %d,"
" \"%c\""
" ]"
" }"
"}"
"";
The regex expressions were developed using: regex101.com. Generated "C" code was tested using onlinegdb.com as well as Google Colab Examples and running the gcc complier inside example cells. JSON data structures were developed and tested end-to-end using JASOnata.