How to access JSON nested value in C++ using Boost.JSON

#include <boost/json.hpp>

boost::json::value j = /* ... */;
// Access nested value with key "value"
j.at("value").as_string();

Full example:

#include <boost/json.hpp>
#include <iostream>

int main() {
    // Hardcoded JSON string
    const std::string json_str = R"(
        {
            "key": "1234",
            "value": "Hello, world!"
        }
    )";

    // Parse the JSON string
    boost::json::value j = boost::json::parse(json_str);

    // Access the "value" property and print it to stdout
    std::string value = j.at("value").as_string().c_str();
    std::cout << value << std::endl; // Prints "Hello, world!"

    return 0;
}

Compile using

g++ -o json json.cpp -lboost_json