Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to modify one of the values within the JSON file, and save it #1475

Closed
XIAY0009 opened this issue Feb 11, 2019 · 5 comments
Closed

Unable to modify one of the values within the JSON file, and save it #1475

XIAY0009 opened this issue Feb 11, 2019 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@XIAY0009
Copy link

XIAY0009 commented Feb 11, 2019

Dear Nlohmann,

Thanks so much for this JSON library :)

For my program, I am trying to:
Step 1: Read from a JSON file which I call it test2.json into my C++ program (No problem in doing so)
Step 2: Modify the value of one of the variables (No problem in doing so)
Step 3: Saves this value into the original JSON file (Not sure how to do this)
Step 4: Add a new variable into the original JSON file (Not sure how to do this)

I'm sure there is a way to accomplish step 3 and 4, but I am not sure how, would really appreciate your help

Please see my simple code below. Currently when I change the value of variable "pi" and write into my original file, it seems to overwrite all my other variables ><

Main program

#include <fstream>
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>

using namespace nlohmann;

int main() {
    std::ifstream in("/LearnDirectory/LearnJSON/test2.json");
    json file = json::parse(in);

    //auto size = file.size();
    //file["size"] = size;
    std::cout << file.at("pi") << '\n';
    std::cout << std::setw(4) << file << '\n';
    float pi_value = file.at("pi");
    std::cout << "Value of pi is: " << pi_value << '\n';
    //Tries o access the object value
    std::cout << file.at("object") << '\n';
    auto& array = file.at("object");
    for (auto&& val:array){
        std::cout << val << '\n';
    }
    std::ofstream o("pretty.json");
    o << std::setw(4) << file << std::endl;

    std::ofstream out("/LearnDirectory/LearnJSON/test2.json");  
    file.at("pi") = 20;  
    out << file.at("pi") << std::endl;  
}   

test2.json

{
    "answer": {
        "everything": 42
    },
    "happy": true,
    "list": [
        1,
        0,
        2
    ],
    "name": "Niels",
    "nothing": null,
    "object": {
        "currency": "USD",
        "value": 42.99
    },
    "pi": 3.142
}
@XIAY0009
Copy link
Author

XIAY0009 commented Feb 11, 2019

I figured out how to do it :)
For the benefit of others, showing it here:

int main() {
    std::ifstream in("/LearnJSON/test2.json");
    json file = json::parse(in);

    std::cout << file.at("pi") << '\n';
    std::cout << std::setw(4) << file << '\n';
    float pi_value = file.at("pi");
    std::cout << "Value of pi is: " << pi_value << '\n';
    //Tries o access the object value
    std::cout << file.at("object") << '\n';
    auto& array = file.at("object");
    for (auto&& val:array){
        std::cout << val << '\n';
    }
    std::ofstream o("pretty.json");
    o << std::setw(4) << file << std::endl;

    json j;
    j = file;
    j["unhappy"] = false;

    std::ofstream out("/LearnJSON/test2.json");
    j["pi"] = 3.14159;
    out << std::setw(4) << j << std::endl;
    float test = j["pi"];
    std::cout << "New variable is: " << test << std::endl;

} 

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Feb 13, 2019
@atulav12
Copy link

Where can I get JSON library ?

@nlohmann
Copy link
Owner

@atulav12
Copy link

Thank you !!!

@user0921
Copy link

user0921 commented Jan 29, 2020

Hi, I'm new to c++ and trying to use nlohmann library and I'm quite stuck. I want to modify array of objects from json. json = { "a": "xxxx", "b": [ { "c": "aaa", "d": [{ "e": "yyy" }, { "e": "sss", "f": "fff" } ] } ] } now I want to replace "e" value with "example" from the above following structure. Could some one help me. I tried to loop through the json structure and was able to read the "e" value but can't replace it. Kindly help me. Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants