Avatar of Rama Tito
Rama Tito
Flag for Malaysia asked on

convert multiple variable values into json format in c programming

 // New temperature readings - 0 ~ 100C
    temp_value = analogRead(ANALOG_PIN_0);
    temp_value = (temp_value/4096) * 100;

    // New temperature readings - 0 ~ 300 PSI
    press_value = analogRead(ANALOG_PIN_1);
    press_value = (press_value/4096) * 300;
    void GET_METER() {     // Update read all data
       for (char i = 0; i < Total_of_Reg ; i++){
           DATA_METER [i] = Read_Meter_float(ID_meter, Reg_addr[i]);// ID_METER_ALL=X

Open in new window

//New EnergyData
   Data_meter[0] = 241.25        //voltage
   Data_meter[1] = 3.53            //Ampere
   Data_meter[2] = 49.1            //kw
   Data_meter[3] = 0.98           //pf
   Data_meter[4] = 49.99         //freq
   Data_meter[5] = 120.58      //kw

   temp_value = 55.3                //Celsius
   press_value = 300.1             //pressure in psi

I am looking forward to send data in jason format. Referring to my code: -

data_send = "{
                              "temp_value" : 55.3,              
                              "press_value": 300.1,
                              "Voltage" : 241.25,
                              "Ampere": 3.53,
                              "KW": 49.1,
                              "pf": 0.98,
                              "freq": 49.99,

How to convert data_send into string format.

Avatar of undefined
Last Comment

8/22/2022 - Mon
Mihai Barbos

I would use a json parser. There are many listed at json.org
Rama Tito

Before i do try send the entire code, I did try with 2 parameters as follows: -

data_send = "{\"temp_value\":"&temp_value&",\"press_value\":"&press_value&"}";

Open in new window

error: invalid operands of types 'const char [15]' and 'float' to binary 'operator&'

     data_send = "{\"temp_value\":"&temp_value&",\"press_value\":"&press_value&"}";
Fabrice Lambert


In C language, you need the strcat function to concatenate strings.
Be warned that the resulting string must have a sufficient size.

Also, the sprintf function will allow you to cast float to strings.

Side note:
Since the data in your array do not have the same meaning, it is better to put them in a structure.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

if using c++ compiler you could do

#include <sstream>

std::ostringstream data_send;
data_send << "{\"temp_value\":" << temp_value << ",\"press_value\":" << press_value <<"}";
send(data_send.str().c_str(), ...);   // send data as const char * 

Open in new window

what is pretty much the same as in your snippet.

to get the floats formatted - say to 3 decimals - you would add

#include <sstream>
#include <iomanip>

std::ostringstream data_send;
data_send << "{\"temp_value\":" << std::fixed << std::setprecision(3) << temp_value
                   << ",\"press_value\":" << std::fixed << std::setprecision(3) << press_value <<"}";[/code]

if using a c compiler you can't overload the stream operator but use functions instead.

#include <stdio.h>
#include <string.h>

int stradd(char buf[], int siz, int pos, const char * psz)
    int len = strlen(psz);
    if (pos + len >= siz)
        return -1;
    strncpy(&buf[pos], psz, len);
    return pos + len;

char * dbl2str(double dbl)
    static char num[32] = { 0 };
    int pos = 0;
    char * p = num;
    sprintf(num, "%9.3f", dbl);
    p = strpbrk(num, "-0123456789.");
    return p;

int main()
    char data_send[4096] = { '\0' };
    char num[32] = { '\0' };
    int pos = 0;
    int siz = (int)sizeof(data_send);
    float temp_value = (float)987.6543;
    float press_value = (float)-0.12;

    pos = stradd(data_send, siz, pos, "{\"temp_value\":");
    pos = stradd(data_send, siz, pos, dbl2str(temp_value));
    pos = stradd(data_send, siz, pos, ",\"press_value\":");
    pos = stradd(data_send, siz, pos, dbl2str(press_value));
    pos = stradd(data_send, siz, pos, "}");
    printf("\n%s\n", data_send);
    return 0;

Open in new window

note, you also could use sprintf to build the send buffer. but sprintf is not safe as the compiler neither can check whether the size of the buffer is big enough nor whether the arguments are matching the format specifiers.

Mihai Barbos

@sara: you have snprintf...

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.