Creating JSON files from scratch
-
@marlenet15
Hello.Is it possible to create a JSON file from scratch?
How do you mean? You could add placeholders for the values and do
QString::replace
on them, but really depends on what you want to accomplish. Do you mind elaborating a bit?Kind regards.
-
@kshegunov Hi. I meant like having a blank text file where you add the sections, keys and values. So something like:
JSON file add section "USER" and add section "PREFERENCES"
In the "USER" section add key "DOB_DAY" with initial value of "0". -
@marlenet15 Hi! You can use the QJson classes for this, see: JSON support in Qt.
-
@marlenet15
I see, well there are several ways to do that. See for example @Wieland's suggestion, it's one of the better ways to manage JSON objects programmatically. Another possible thing you could do is to have a "template" file, as per my first post, and after reading that file to replace the placeholders with the actual values. It's a matter of preference and convenience mostly and what you want to do after you get the correct data. So as to the "placeholder approach" (the other one is well described within the documentation), you could use something along the lines of:QFile file("template.json"). if (!file.open(QFile::ReadOnly | QFile::Text)) ; // Don't forget to handle errors QTextStream in(&file); QString fileContents = in.readAll(); //< Read the whole file into a QString QStringList placeholders = QStringList() << "%DOB_DAY%" << "%DOB_MONTH%"; // ... and so on QStringList data = QStringList() << "0" << "1"; // ... and so on Q_ASSERT(placeholders.size() == data.size()); // Make sure both lists are the same size for (qint32 i = 0, size = placeholders.size(); i < size; i++) fileContents.replace(placeholders[i], data[i]);
This'd replace the placeholders in a file of this liking:
{ "USER": { "DOB_DAY": "%DOB_DAY%", "DOB_MONTH": "%DOB_MONTH%", ... } }
Do note, however, that this implementation is quite inefficient, so you're probably better off using what @Wieland sourced.
Kind regards.
-
@Wieland said:
@marlenet15 Hi! You can use the QJson classes for this, see: JSON support in Qt.
Just to demonstrate that option:
QJsonObject user; user["DOB_DAY"] = 0; user["DOB_MONTH"] = 0; user["DOB_YEAR"] = 0; user["FIRST_NAME"] = "name"; user["GENDER"] = "gender"; user["LAST_NAME"] = "last"; user["MIDDLE_INITIAL"] = "mi"; QJsonObject preferences; preferences["USER_NAME"] = "username"; preferences["COLOR"] = "color"; preferences["CITY"] = "city"; QJsonObject jsonObject; jsonObject["USER"] = user; jsonObject["PREFERENCES"] = preferences; QJsonDocument doc(jsonObject); qDebug() << doc.toJson();
Then you can write the output of
doc.toJson()
(a QByteArray) wherever you like, including to a file.In the above example,
doc.toJson()
produces:{ "PREFERENCES": { "CITY": "city", "COLOR": "color", "USER_NAME": "username" }, "USER": { "DOB_DAY": 0, "DOB_MONTH": 0, "DOB_YEAR": 0, "FIRST_NAME": "name", "GENDER": "gender", "LAST_NAME": "last", "MIDDLE_INITIAL": "mi" } }
Note the
USER/DOB_*
properties are are JSON numbers, and not JSON strings. If you really wanted them to be strings (as per your OP example) then just wrap the0
values in quotes (orQString
, etc) in the source.Cheers.
-
@kshegunov The problem is that the template needs to be used multiple times. In other words, I need to create several json files with that exact same templates since there could be many users.
-
@marlenet15 said:
The problem is that the template needs to be used multiple times.
Is the template something you define, or an existing file that you want to update / modify?
Where are you getting the values to populate the template(s) from?
-
@Paul-Colby I would rather have no template and be able to create the keys and values of the JSON file within Qt since I want a new JSON file to be created every time I have a new user.
-
@marlenet15 said:
create the keys and values of the JSON file within Qt since I want a new JSON file to be created every time I have a new user.
So why not wrap up the code example above into a function, and call it whenever you get a new user? You can pass all of the new user's details into the function, and have it write to a filename specific to the user etc.
Is that the sort of thing you are meaning?
-
@Paul-Colby YES!! I can't believe I didn't see it. I guess I didn't read it carefully. Thank you!!!!