Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating JSON files from scratch

Creating JSON files from scratch

Scheduled Pinned Locked Moved Solved General and Desktop
jsoncreate
11 Posts 4 Posters 6.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M marlenet15

    Is it possible to create a JSON file from scratch? I have been looking everywhere and I couldn't find any examples. I would like to create the example below. The reason is each user will have their own file and there could be many users.

    {
        "USER": {
            "DOB_DAY": "0",
            "DOB_MONTH": "0",
            "DOB_YEAR": "0",
            "FIRST_NAME": "name",
            "GENDER": "gender",
            "LAST_NAME": "last",
            "MIDDLE_INITIAL": "mi",
        },
    
        "PREFERENCES": {
    	"USER_NAME": "username",
    	"COLOR": "color",
    	"CITY": "city",
        }
    }
    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #2

    @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.

    Read and abide by the Qt Code of Conduct

    M 1 Reply Last reply
    0
    • kshegunovK kshegunov

      @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.

      M Offline
      M Offline
      marlenet15
      wrote on last edited by
      #3

      @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".

      kshegunovK 1 Reply Last reply
      0
      • M marlenet15

        Is it possible to create a JSON file from scratch? I have been looking everywhere and I couldn't find any examples. I would like to create the example below. The reason is each user will have their own file and there could be many users.

        {
            "USER": {
                "DOB_DAY": "0",
                "DOB_MONTH": "0",
                "DOB_YEAR": "0",
                "FIRST_NAME": "name",
                "GENDER": "gender",
                "LAST_NAME": "last",
                "MIDDLE_INITIAL": "mi",
            },
        
            "PREFERENCES": {
        	"USER_NAME": "username",
        	"COLOR": "color",
        	"CITY": "city",
            }
        }
        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #4

        @marlenet15 Hi! You can use the QJson classes for this, see: JSON support in Qt.

        Paul ColbyP 1 Reply Last reply
        0
        • M marlenet15

          @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".

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #5

          @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.

          Read and abide by the Qt Code of Conduct

          M 1 Reply Last reply
          1
          • ? A Former User

            @marlenet15 Hi! You can use the QJson classes for this, see: JSON support in Qt.

            Paul ColbyP Offline
            Paul ColbyP Offline
            Paul Colby
            wrote on last edited by
            #6

            @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 the 0 values in quotes (or QString, etc) in the source.

            Cheers.

            1 Reply Last reply
            3
            • kshegunovK kshegunov

              @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.

              M Offline
              M Offline
              marlenet15
              wrote on last edited by
              #7

              @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.

              1 Reply Last reply
              0
              • Paul ColbyP Offline
                Paul ColbyP Offline
                Paul Colby
                wrote on last edited by
                #8

                @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?

                M 1 Reply Last reply
                1
                • Paul ColbyP Paul Colby

                  @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?

                  M Offline
                  M Offline
                  marlenet15
                  wrote on last edited by
                  #9

                  @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.

                  1 Reply Last reply
                  0
                  • Paul ColbyP Offline
                    Paul ColbyP Offline
                    Paul Colby
                    wrote on last edited by
                    #10

                    @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?

                    M 1 Reply Last reply
                    1
                    • Paul ColbyP Paul Colby

                      @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?

                      M Offline
                      M Offline
                      marlenet15
                      wrote on last edited by
                      #11

                      @Paul-Colby YES!! I can't believe I didn't see it. I guess I didn't read it carefully. Thank you!!!!

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved