0

I have a text file of format given below (this is just sample file, but actually file is big one with key, values). Some of the values for the key will have format in file (look for value of Port key in below file). This is input file for me.

I am converting this file into Json using CJson Library in C++. Since value corresponding to Port key is not a valid string or value, CJson gives error message saying "Insert missing quotes around strings". So I tried to convert Port value to string using below code. But I am unable to insert missing quotes around. Could you please help me how to "Insert missing quotes around strings".

Code I tried:

ifstream infile { "conf_file.conf" };
string file_contents{ istreambuf_iterator<char>(infile), istreambuf_iterator<char>() };
pParentJson = cJSON_Parse( (char*)file_contents.c_str() );
cJSON* pJsonObj = cJSON_GetObjectItem(pParentJson,"port");
char* pDataBuffer = cJSON_Print(pJsonObj);
cout<<pDataBuffer<<endl;

Text File

{
"port": <CbTcpPortVariable>
"IP" : "127.0.0.1"
"Message": "Hello"
}
Shubham
  • 628
  • 1
  • 9
  • 19
  • if `` is the only issue, then you can probably just [find-and-replace](https://stackoverflow.com/questions/5878775/how-to-find-and-replace-string) it before parsing as json – kmdreko May 20 '20 at 05:05
  • @kmdreko this is just a example. I will have too many such values in file, names may different here. I am trying to understand that if it is possible to read the values and convert it into string. – Masthan Vali Syed May 20 '20 at 05:11
  • @MasthanValiSyed: The format of the input text file (`.conf`) is not JSON, right? – Azeem May 20 '20 at 05:23
  • @Azeem, you are correct. – Masthan Vali Syed May 20 '20 at 05:29
  • @MasthanValiSyed: Right. Why do you parse it then? In your posted code, after reading `file_contents`, you're calling `cJSON_Parse()` on it. But, the input data is not valid JSON. Can you elaborate on that please? – Azeem May 20 '20 at 05:32

0 Answers0