Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using nlohmann JSON to insert more data

Using nlohmann JSON to insert more data

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++pythondata-structuresjson
2 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kiran Satish
    wrote on last edited by
    #1

    I have a json file genrated in python from another system that needs to be read/updated in another MFC application. I can successfully read the existing data from the json file using nlohmann json add-in. For example, the file consists of two data fields with one field having any array of two subfields as below

    {
    "city_data":[
    {
    "t":"m",
    "l":[12.0,10.3,0.0,1.0]
    },
    {
    "t":"l",
    "l":[10.1,20.37,0.0,1.0]
    },
    {
    "t":"l",
    "l":[47.82,4.63,0.0,1.0]
    },
    {
    "t":"m",
    "l":[67.66,43.33,0.0,1.0]
    }
    ],
    "map_data":"JZDKZTCaTyWQymUwmk8lkMplMJpPJZDKZTCaTyWQymUwmk/8/+n8AVAZ1WCxk8rYAAAAASUVORK5CYII="
    }

    The "map_data" is basically base64 encoded png image. I would like to add more entries into "city_data" field as {"t":"x","l":[0.0,0.0,0.0,0.0]} while the "map_data" stays the same. Then I can write the json object into a new file with updates. To read the json file,

    std::ifstream f(file);
    json data = json::parse(f);

    To retreive "city_data" and then its type and location

    json& cityInfo = data["city_data"];
    for (unsigned int i = 0; i < cityInfo.size(); i++)
    {
    json& cityType = cityInfo[i];
    std::string ctyTyp = cityType["t"];
    json& cityLoc = cityType["l"];
    std::float_t x = cityLoc[0];
    std::float_t y = cityLoc[1];
    // do something with the retrieved values
    }
    //To retreive map data
    ofstream outfile;
    outfile.open(m_strFolderPath + m_strFileName + ".png", ofstream::binary);
    std::string mapFrame = data["map_data"];
    string temp = base64_decode(mapFrame );
    outfile.write(temp.c_str(), temp.size());
    outfile.close();
    f.close();

    Any suggestions on how to insert new city_data fields into the nlohmann JSON object? or a way to create a new json file like in above format? thanks

    PKNT

    Mircea NeacsuM 1 Reply Last reply
    0
    • K Kiran Satish

      I have a json file genrated in python from another system that needs to be read/updated in another MFC application. I can successfully read the existing data from the json file using nlohmann json add-in. For example, the file consists of two data fields with one field having any array of two subfields as below

      {
      "city_data":[
      {
      "t":"m",
      "l":[12.0,10.3,0.0,1.0]
      },
      {
      "t":"l",
      "l":[10.1,20.37,0.0,1.0]
      },
      {
      "t":"l",
      "l":[47.82,4.63,0.0,1.0]
      },
      {
      "t":"m",
      "l":[67.66,43.33,0.0,1.0]
      }
      ],
      "map_data":"JZDKZTCaTyWQymUwmk8lkMplMJpPJZDKZTCaTyWQymUwmk/8/+n8AVAZ1WCxk8rYAAAAASUVORK5CYII="
      }

      The "map_data" is basically base64 encoded png image. I would like to add more entries into "city_data" field as {"t":"x","l":[0.0,0.0,0.0,0.0]} while the "map_data" stays the same. Then I can write the json object into a new file with updates. To read the json file,

      std::ifstream f(file);
      json data = json::parse(f);

      To retreive "city_data" and then its type and location

      json& cityInfo = data["city_data"];
      for (unsigned int i = 0; i < cityInfo.size(); i++)
      {
      json& cityType = cityInfo[i];
      std::string ctyTyp = cityType["t"];
      json& cityLoc = cityType["l"];
      std::float_t x = cityLoc[0];
      std::float_t y = cityLoc[1];
      // do something with the retrieved values
      }
      //To retreive map data
      ofstream outfile;
      outfile.open(m_strFolderPath + m_strFileName + ".png", ofstream::binary);
      std::string mapFrame = data["map_data"];
      string temp = base64_decode(mapFrame );
      outfile.write(temp.c_str(), temp.size());
      outfile.close();
      f.close();

      Any suggestions on how to insert new city_data fields into the nlohmann JSON object? or a way to create a new json file like in above format? thanks

      PKNT

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      I haven't used nlohmann json in a while (because I've made my own - see mlib/json.h at master · neacsum/mlib · GitHub[^] However I think this should work:

      // do something with the retrieved values
      cityInfo\[i\]\["newval"\] = to\_string (i + 1);
      

      --EDIT-- I read your message again and realized you want to extend the cityInfo array. In this case you would simply write something like:

      cityInfo[4] = json::parse(R"({"t":"x", "l" : [0.0, 0.0, 0.0, 0.0] })");

      Mircea

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups