<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Fetching current json adding to it and storing it back in the file]]></title><description><![CDATA[<p dir="auto">im doing something wrong here but i can't see what!! so im trying to read the json file and add to it and then store it back into the json file but for some reason it dont add/append to the current json it completely rewrites it with that 1 entry...</p>
<pre><code class="language-c++">bool DeviceData::devicedataupdated()
{
    DeviceDataDebugger.LevelInfo("DEVICEDATA", "Adding New Device", "!");

    QFile deviceListFile(_JsonFile);

    if( !deviceListFile.open( QIODevice::ReadOnly ) )
    {
        DeviceDataDebugger.LevelError("DEVICEDATA", "Could not read the json file", "!");
        return false;
    }

    QJsonDocument jsonOrg = QJsonDocument::fromJson( deviceListFile.readAll() );

    deviceListFile.close();

    if (deviceListFile.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QJsonObject newDevice;

        newDevice.insert("IPA",      "newdevice");
        newDevice.insert("Icon",     "fa_bolt");
        newDevice.insert("Name",     "New Device");
        newDevice.insert("Status",   "online");
        newDevice.insert("Type",     "Strip");
        newDevice.insert("UVLights", "false");

        QJsonArray devices = jsonOrg.array();

        devices.push_back(newDevice);

        QJsonDocument jsonDoc( devices );

        QJsonObject obj = jsonDoc.object();

        deviceListFile.write(jsonDoc.toJson());

        deviceListFile.close();
        return true;
    }
    return false;
}
</code></pre>
<p dir="auto">This is how it looks befor running the code above (and how it should look with the object "Device")</p>
<pre><code>{
    "Device": [
        {
            "IPA": "studiolights",
            "Icon": "fa_bolt",
            "Name": "Studio Lights",
            "Status": "online",
            "Type": "Strip",
            "UVLights": "false"
        }
    ]
}
</code></pre>
<p dir="auto">This is after</p>
<pre><code>[
    {
        "IPA": "newdevice",
        "Icon": "fa_bolt",
        "Name": "New Device",
        "Status": "online",
        "Type": "Strip",
        "UVLights": "false"
    }
]
</code></pre>
]]></description><link>https://forum.qt.io/topic/130127/fetching-current-json-adding-to-it-and-storing-it-back-in-the-file</link><generator>RSS for Node</generator><lastBuildDate>Wed, 06 May 2026 17:32:11 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/130127.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 08 Sep 2021 10:05:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 12:01:56 GMT]]></title><description><![CDATA[<p dir="auto">The complete answer :)</p>
<pre><code>bool DeviceData::devicedataupdated()
{
    QFile deviceListFile(_JsonFile);

    if( !deviceListFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
    {
        return false;
    }

    QJsonDocument jsonOrg = QJsonDocument::fromJson( deviceListFile.readAll() );

    deviceListFile.close();

    if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
    {
        QJsonObject newDevice;

        newDevice.insert( "IPA",      "newdevice" );
        newDevice.insert( "Icon",     "fa_bolt" );
        newDevice.insert( "Name",     "New Device" );
        newDevice.insert( "Status",   "online" );
        newDevice.insert( "Type",     "Strip" );
        newDevice.insert( "UVLights", "false" );

        auto top = jsonOrg.object();

        auto devices = top.value( "Device" ).toArray();

        devices.push_back( newDevice );

        top.insert("Device", devices);

        QJsonDocument jsonDoc( top );

        deviceListFile.write( jsonDoc.toJson() );

        deviceListFile.close();
        return true;
    }

    return false;
}
</code></pre>
]]></description><link>https://forum.qt.io/post/679733</link><guid isPermaLink="true">https://forum.qt.io/post/679733</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 12:01:56 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 12:00:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> there we go :)</p>
]]></description><link>https://forum.qt.io/post/679732</link><guid isPermaLink="true">https://forum.qt.io/post/679732</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 12:00:15 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:57:13 GMT]]></title><description><![CDATA[<p dir="auto">Ah sorry, it's working on a copy of the array, obviously. You need to also add <code>devices</code> back to <code>top</code>:</p>
<pre><code>top.insert("Device", devices);
</code></pre>
]]></description><link>https://forum.qt.io/post/679731</link><guid isPermaLink="true">https://forum.qt.io/post/679731</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:57:13 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:54:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a></p>
<pre><code class="language-c++">    if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
    {
        QJsonObject newDevice; // projectDetails

        newDevice.insert("IPA",      "newdevice");
        newDevice.insert("Icon",     "fa_bolt");
        newDevice.insert("Name",     "New Device");
        newDevice.insert("Status",   "online");
        newDevice.insert("Type",     "Strip");
        newDevice.insert("UVLights", "false");

        auto top = jsonOrg.object();

        QJsonArray devices = top.value("Device").toArray();

        devices.push_back(newDevice);

        QJsonDocument jsonDoc( top );

        deviceListFile.write(jsonDoc.toJson());

        deviceListFile.close();
        return true;
    }
</code></pre>
<p dir="auto">does nothing! like the file remains as it was :/ no new entry was added when running the code nor any error!</p>
]]></description><link>https://forum.qt.io/post/679730</link><guid isPermaLink="true">https://forum.qt.io/post/679730</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:54:39 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:47:40 GMT]]></title><description><![CDATA[<p dir="auto">Hey wait. The jsonOrg is not an array! You have a top-level object there called <code>"Device"</code>. So that's why this code is failing for you.</p>
<pre><code>auto top = jsonOrg.object();
auto devices = top.value("Device").toArray();
[...]
QJsonDocument jsonDoc( top );
</code></pre>
<p dir="auto">With that it should work.</p>
]]></description><link>https://forum.qt.io/post/679729</link><guid isPermaLink="true">https://forum.qt.io/post/679729</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:47:40 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:41:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> so now the only part is this</p>
<pre><code class="language-c++">    if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
    {
        QJsonObject newDevice;

        newDevice.insert("IPA",      "newdevice");
        newDevice.insert("Icon",     "fa_bolt");
        newDevice.insert("Name",     "New Device");
        newDevice.insert("Status",   "online");
        newDevice.insert("Type",     "Strip");
        newDevice.insert("UVLights", "false");

        QJsonArray devices = jsonOrg.array();

        devices.push_back(newDevice);

        QJsonDocument jsonDoc( devices );

        QJsonObject obj = jsonDoc.object();

        deviceListFile.write(jsonDoc.toJson());

        deviceListFile.close();
        return true;
    }
</code></pre>
]]></description><link>https://forum.qt.io/post/679728</link><guid isPermaLink="true">https://forum.qt.io/post/679728</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:41:16 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:31:56 GMT]]></title><description><![CDATA[<p dir="auto">Of course it clears the file but that has no consequence - you are building a new JSON will all the data in it, and then save it. So it's good that at that point the file is empty.</p>
]]></description><link>https://forum.qt.io/post/679726</link><guid isPermaLink="true">https://forum.qt.io/post/679726</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:31:56 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:31:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> so by <code>qDebug() &lt;&lt; "File Readl All : " &lt;&lt; jsonOrg;</code> i got</p>
<pre><code>File Readl All :  QJsonDocument({"Device":[{"IPA":"studiolights","Icon":"fa_bolt","Name":"Studio Lights","Status":"online","Type":"Strip","UVLights":"false"}]})
Error :  "no error occurred" 0
</code></pre>
]]></description><link>https://forum.qt.io/post/679723</link><guid isPermaLink="true">https://forum.qt.io/post/679723</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:31:08 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:23:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kris-revi">@<bdi>Kris-Revi</bdi></a> said in <a href="/post/679718">Fetching current json adding to it and storing it back in the file</a>:</p>
<blockquote>
<p dir="auto">deviceListFile.readAll();</p>
</blockquote>
<p dir="auto">Except: make sure you call <code>readAll()</code> <strong>ONCE</strong> - store it in a variable, then print with qDebug(), then pass to the logic below it.</p>
<p dir="auto">If you call it twice, the second call will likely be empty.</p>
]]></description><link>https://forum.qt.io/post/679722</link><guid isPermaLink="true">https://forum.qt.io/post/679722</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:23:48 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:23:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> i JUST did :| the file exists and has stuff in it :S</p>
]]></description><link>https://forum.qt.io/post/679721</link><guid isPermaLink="true">https://forum.qt.io/post/679721</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:23:18 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:22:49 GMT]]></title><description><![CDATA[<p dir="auto">OK, case clear. The file is empty :-) Verify the path, and open the file in system to see if it's true.</p>
]]></description><link>https://forum.qt.io/post/679720</link><guid isPermaLink="true">https://forum.qt.io/post/679720</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:22:49 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:18:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a></p>
<pre><code class="language-c++">qDebug() &lt;&lt; "File Readl All : " &lt;&lt; deviceListFile.readAll();
qDebug() &lt;&lt; "Error : " &lt;&lt; error-&gt;errorString() &lt;&lt; error-&gt;offset;
</code></pre>
<pre><code>File Readl All :  ""
Error :  "no error occurred" 0
</code></pre>
<p dir="auto">it just comes up empty :S</p>
]]></description><link>https://forum.qt.io/post/679718</link><guid isPermaLink="true">https://forum.qt.io/post/679718</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:18:39 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:07:18 GMT]]></title><description><![CDATA[<p dir="auto">Then at least we know the problem lies in file reading, not in saving.</p>
<p dir="auto">Check what <code>readAll()</code> actually returns. Plus add error checking to JSON document (add and check QJsonParseError to your <code>fromJson()</code> call).</p>
]]></description><link>https://forum.qt.io/post/679716</link><guid isPermaLink="true">https://forum.qt.io/post/679716</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:07:18 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 11:02:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> said in <a href="/post/679697">Fetching current json adding to it and storing it back in the file</a>:</p>
<blockquote>
<p dir="auto">QJsonArray devices = jsonOrg.array();</p>
<p dir="auto">Add a log here (or look it up in debugging session) to see if there is actually any data read from the file.</p>
</blockquote>
<p dir="auto">hmmm the print out comes up empty :S infact it does not print at all</p>
<pre><code class="language-c++">        for(int i = 0; i &lt; devices.size(); i++)
            qDebug() &lt;&lt; "Json Original : " &lt;&lt; devices[i];
</code></pre>
]]></description><link>https://forum.qt.io/post/679714</link><guid isPermaLink="true">https://forum.qt.io/post/679714</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 11:02:55 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 10:44:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kris-revi">@<bdi>Kris-Revi</bdi></a> said in <a href="/post/679707">Fetching current json adding to it and storing it back in the file</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> wait <code>QFile::ReadOnly</code> &amp; <code>QFile::Text</code> i thought it was <code>QIODevice::ReadOnly</code> and <code>QIODevice::Text</code></p>
</blockquote>
<p dir="auto">Doesn't matter it's the same enum and the values are the same. I prefer using QFile because it's shorter and more consistent.</p>
]]></description><link>https://forum.qt.io/post/679710</link><guid isPermaLink="true">https://forum.qt.io/post/679710</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 08 Sep 2021 10:44:21 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 10:43:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> wait <code>QFile::ReadOnly</code> &amp; <code>QFile::Text</code> i thought it was <code>QIODevice::ReadOnly</code> and <code>QIODevice::Text</code></p>
]]></description><link>https://forum.qt.io/post/679707</link><guid isPermaLink="true">https://forum.qt.io/post/679707</guid><dc:creator><![CDATA[Kris Revi]]></dc:creator><pubDate>Wed, 08 Sep 2021 10:43:05 GMT</pubDate></item><item><title><![CDATA[Reply to Fetching current json adding to it and storing it back in the file on Wed, 08 Sep 2021 10:22:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kris-revi">@<bdi>Kris-Revi</bdi></a> said in <a href="/post/679694">Fetching current json adding to it and storing it back in the file</a>:</p>
<blockquote>
<p dir="auto">if( !deviceListFile.open( QIODevice::ReadOnly ) )<br />
{</p>
</blockquote>
<p dir="auto">You are opening the file without text mode.</p>
<pre><code>if( !deviceListFile.open( QFile::ReadOnly | QFile::Text ) )
</code></pre>
<blockquote>
<p dir="auto">QJsonArray devices = jsonOrg.array();</p>
</blockquote>
<p dir="auto">Add a log here (or look it up in debugging session) to see if there is actually any data read from the file.</p>
]]></description><link>https://forum.qt.io/post/679697</link><guid isPermaLink="true">https://forum.qt.io/post/679697</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Wed, 08 Sep 2021 10:22:17 GMT</pubDate></item></channel></rss>