Is there a way find a key in the whole json file using QJsonObject?

Is there a way find a key in the whole json file using QJsonObject?

By : Yan Imanita Abdilah
Date : October 17 2020, 06:10 AM
like below fixes the issue Generally, this can be solved with a recursive function (a function which calls itself). We first pass it the document's object, then check the object's keys. If no keys are found, we will apply the same function on the values of each key. If an array is passed, we should iterate through it.
code :
QJsonValue findKey(const QString& key, const QJsonValue& value) {
    if (value.isObject()) {
        const QJsonObject obj = value.toObject();
        if (obj.contains(key))
            return obj.value(key);           // return 'early' if object contains key    

        for (const auto& value : obj) {
            QJsonValue recurse = findKey(key, value);  // call itself, forwarding a value        
            if (!recurse.isNull())
                return recurse;              // value found, return 'early'
    } else if (value.isArray()) {
        for (const auto& value : value.toArray()) {
            QJsonValue recurse = findKey(key, value);
            if (!recurse.isNull())
                return recurse;

    return QJsonValue();          // base case: a null value

int main(int argc, char *argv[])
    QFile file(":/res/scratch.json");   // json stored in a qrc with /res/ prefix

    if (!file.isOpen()) {
        qDebug() << "error: couldn't open scratch.json";
        return 0;

    QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
    qDebug() << "value:" << findKey("treasure", doc.object());
    "deck": [
        "first mate",
        "john muir"
    "cabin": [
            "name": "lamp"
            "name": "treasure chest",
            "items": {
                "diamonds": 3,
                "silver": 5,
                "gold": 10,
                "safebox": {
                    "treasure": "shiny"
value: QJsonValue(string, "shiny")

Share : facebook icon twitter icon
Qt parsing JSON using QJsonDocument, QJsonObject, QJsonArray

Qt parsing JSON using QJsonDocument, QJsonObject, QJsonArray

By : dave623
Date : March 29 2020, 07:55 AM
help you fix your problem I'm using Qt5. I am trying to obtain values from a json object. Here is what the json object looks like that I am trying to get data from: , I figured it out:
code :
QStringList propertyNames;
QStringList propertyKeys;
QString strReply = (QString)reply->readAll();
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
QJsonArray jsonArray = jsonObject["properties"].toArray();

foreach (const QJsonValue & value, jsonArray) {
    QJsonObject obj = value.toObject();
Read JSON with QJsonObject

Read JSON with QJsonObject

By : Никита Корнишев
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I'm writing a couple test functions as it's my first time with Qt and trying to understand the bits I need to develop my end project. Here are the functions: , You forget to include QJsonObject:
code :
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
Find a node in JSON file, update the content and save back into the JSON file using c#

Find a node in JSON file, update the content and save back into the JSON file using c#

By : user1570606
Date : March 29 2020, 07:55 AM
will be helpful for those in need Current JSON file looks like this , Try this:
code :
public static void Update(Client c, string s)
    var json = File.ReadAllText(fileName);

    List<Client> list = JsonConvert.DeserializeObject<List<Client>>(json);

    Client found = list.Where(x => x.Name == s).Single();

    found.Name = c.Name;
    found.Path = c.Path;

    var updatedJson = JsonConvert.SerializeObject(list);
    File.WriteAllText(fileName, updatedJson);
Read a json file, find and replace double quotes and write new json file

Read a json file, find and replace double quotes and write new json file

By : boschista
Date : March 29 2020, 07:55 AM
Hope this helps I have a JSON file which looks like: , Use ast module
code :
import json
with open('path/to/file.json','r',encoding ='utf-8') as f:
    data = json.load(f)

for item in data:
    item['ner'] = ast.literal_eval(item['ner'])

with open('path/to/output_file.json'', 'w') as f:
    json.dump(data, f)
Get value from QJsonObject by index

Get value from QJsonObject by index

By : Andrey_WeeTeam
Date : March 29 2020, 07:55 AM
this will help first suggestion : maybe you can use this code instead, which is more readable
Related Posts Related Posts :
  • why it is not necessary to use this keyword for pointer?
  • C++ issue with new char[]
  • Force gcc not to inline a function
  • calling child class function
  • C/C++ function definitions without assembly
  • Are there any lint tools for C and C++ that check formatting?
  • C++0x - export gone, exception specs deprecated. Will this affect your code?
  • How to load image data from resource bitmap file for directshow filter?
  • Const operator overloading problems in C++
  • Super Noob C++ variable help
  • Virtual functions and polymorphism
  • C++ language some live examples for mutable
  • What's wrong with my code about find the max one and the min one by vector
  • Force g++ to indicate when library is not included
  • expected unqualified-id before ‘or’ token
  • signed int vs int - is there a way to tell them apart in C++?
  • When does an asio timer go out of scope?
  • Locating objects (structs) in memory - how to?
  • GoogleTest: Accessing the Environment from a Test
  • Visual C++.NET , speed optimizations
  • Different cursor formats in IOFrameBufferShared
  • openssl versus windows capi
  • Top level window on X Window System
  • C++ pointer to const pointer
  • Is using macros to abbreviate long winded boost template names a bad practice?
  • How to detect end-of-file when using getline?
  • Converting QXmlItem to QtDomElement or similar?
  • C++ freeing static variables
  • Compiling/Debugging LZMA
  • What is the fastest way to find out the time in the windows with an accuracy of 1ms?
  • Increment order
  • C++0x atomic implementation in c++98 question about __sync_synchronize()
  • C++ - Access array (in main) from methods outside main
  • How can i stream CCTV camera to iphone from windows
  • Function that counts the number of integers in a text file?
  • "Reading" a POD preincrement result does not yield undefined behavior. Why exactly?
  • Can nullptr be emulated in gcc?
  • Swapping one widget with another in Qt
  • Fastest C++ Signal/Slot Lib without dependency
  • Isn't this an error in the book The C++ Programming Language(4 ed)?
  • Error in C++ Vector Usage: No matching member function for call to 'push_back'
  • Can someone tell me why I am unable to find the length of array using sizeof() function here?
  • How to cout a constructor?
  • printf treats *p++ differently from what happens to p
  • How to pass a constexpr array into a function
  • OpenCV building fails due to DirectX
  • How to 'backspace' using a pushbutton
  • Binary literal in condition
  • Access captured variables outside the lambda
  • Storing 4 values from each line from a txt file, into an object - C++
  • What is the most efficient way to test for duplicates in a user inputted array?
  • How to find a string in a binary file?
  • Passing variable into lambda
  • decltype(auto) function return type does not deduce && types
  • Find the unique elements of a vector C++
  • Why doesn't str != "hello" && "goodbye" work?
  • Array rotate and delete
  • Is the concept of release-sequence useful in practice?
  • Multiple spotlights in opengl doesn't work
  • The for loop isn't entered even if the initial requirement is true
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk