How to Minify JSON data in qt?..
-
I have JSON file and reading it as file. Now i want to Minify JSON file content.
[
{
"updated":1592183850402,
"country":"Morocco",
"countryInfo":
{"_id":504,
"iso2":"MA",
"iso3":"MAR",
"lat":32,
"continent":"Africa",
"activePerOneMillion":22.12
}
}
]The output should be
[{"updated":1592183850402,"country":"Morocco","countryInfo":{"_id":504,"iso2":"MA","iso3":"MAR","lat":32,continent":"Africa","activePerOneMillion":22.12}}]
-
@Akash_Patil Use https://doc.qt.io/qt-5/qjsondocument.html#toJson-1 with QJsonDocument::Compact (https://doc.qt.io/qt-5/qjsondocument.html#JsonFormat-enum)...
-
@Akash_Patil Take a look a
QJSonDocument::toJson()
.You can do something like this:
QJSonDocument doc = QJSonDocument::fromJson(jsonString); qDebug() << doc.toJson(QJsonDocument::Compact);
-
@KroMignon I tried the way you have mentioned.
[void readJson() { QFile file; file.setFileName("sample2.json"); file.open(QIODevice::ReadOnly ); QByteArray ba2 = file.readAll(); file.close(); QJsonParseError parseError; QJsonDocument doc = QJsonDocument::fromJson(ba2, &parseError); qDebug() << doc.toJson(QJsonDocument::Compact); }
Input File Data:
{
"firstName": "Joe",
"lastName": "Jackson",
"gender": "male",
"age": 28,
"address": {
"streetAddress": "101",
"city": "San Diego",
"state": "CA"
},
"phoneNumbers": [
{ "type": "home", "number": "7349282382" }
]
}In output there is backslash for every parameter.
"{\"address\":{\"city\":\"San Diego\",\"state\":\"CA\",\"streetAddress\":\"101\"},\"age\":28,\"firstName\":\"Joe\",\"gender\":\"male\",\"lastName\":\"Jackson\",\"phoneNumbers\":[{\"number\":\"7349282382\",\"type\":\"home\"}]}"
-
@Akash_Patil said in How to Minify JSON data in qt?..:
In output there is backslash for every parameter.
I cannot understand what you mean? Which backslash?
Perhaps is it only a debug artifact: you could try to use
qDebug() << qPrintable(doc.toJson(QJsonDocument::Compact));
which removes the additional quotes at beginning and end of the string. -
@Akash_Patil said in How to Minify JSON data in qt?..:
In output there is backslash for every parameter.
The backslash is coming from qDebug(), which is meant to be used for debugging purposes. So, the code works as expected...