Important: Please read the Qt Code of Conduct -

JSON over socket

  • Hello everybody,

    Ok, so, I've had INI file and I've used some functions like childKeys and childGroups to get TAGS,keys and values. After that I've made Json Object that looks like this:


    I've made QJsonDocument and sent it over TCP socket with this code:

    QJsonDocument doc(jsonObj);
    QByteArray jByte(doc.toJson(QJsonDocument::Compact));

    On the client side I want to reverse parse it so I can have TAGS and values seperately which I'm going to put them in some sort of a table or list. Is there any possible way to do it with some kind of reverse functions of childKeys and childGroups?

    Thanks in advance.

  • @YouKnowMe

    I am not sure if Qt has a default way to convert a json to a childgroup format. But you can read the Json and implement yourself way to create it

    QJsonParseError parseError;
    QJsonDocument doc = QJsonDocument::fromJson(socket->readAll(), &parseError);
    if(parseError.error ==  QJsonParseError::NoError){
      // Has a Error
    QJsonObject obj = doc.object();
    // acessing all object Keys
    for(const QString& key : obj.keys()){
       qDebug() << obj[key];

  • @KillerSmath said in JSON over socket:

    childgroup format

    What is meant by childgroup format? I searched online, but I am not seeing a particular concept or pattern.

  • @fcarney
    Sorry if I was not clear. I told about the QSettings.
    I am not sure if Qt has a interface to directly convert Json to Ini format.

  • This post is deleted!

  • @YouKnowMe
    I noticed some syntax errors in my post. It was already fixed.
    Implicit conversion from list to QJsonDocument ? Can you show a snippet of your code ?

  • Ok, more details...
    I had an INI file with tags, keys and values, like this:

    . . .

    On my server side I got them with this code:

    QSettings settings(":/data.ini",QSettings::IniFormat);
    QStringList groups= settings.childGroups();
    foreach (QString group, groups) {
          QStringList keys = settings.childKeys();
          QJsonArray arr;
          foreach (key, keys) {
                   QJsonObject object;
                   QJsonValue valueJson(values.toString());

    As a result I got this Json object:


    Next, I've made QJsonDocument and sent it over TCP socket with the code I wrote in the first post.

    Now on the client side I want to get TAGS and values separately (without name1, name2,...) and put them in a list or a table. Is there a way I can do that?

  • In case you haven't seen this:


    QJsonDocument(jstring).object()  // gets your top level QJsonObject

    From there you are accessing keys inside the object using the keys() call.
    Your hierarchy should look like this for what you have:

    Of course use the tests for isObject, isArray, etc to make sure you have what you think you have.

Log in to reply