Search code examples
c++jsonstringparsingstdvector

From a json object/file, how can I derive the values belonging to keys within an array while storing them as a string within a vector?



Json object/file sample:

{
  "ADMIN_LIST" :[
    {
      "name" : "Luke",
      "age" : 36,
      "id_hash" : "acbfa7acrbad90adb6578adabdff0dcbf80abad43276d79b76f687590390b3ff429"
    },
    {
      "name" : "Sasha",
      "age" : 48,
      "id_hash" : "97acbfa7acrbad90adb6578adabd0dcbf80abad43276d79b76f687590390b3ff429"
    },
    {
      "name" : "Henry",
      "age" : 42,
      "id_hash" : "2acbfa7acrbad90adb6578adabd0dcbf80abad493276d79b76f687590390b3ff429"
    },
    {
      "name" : "Jake",
      "age" : 31,
      "id_hash" : "facbfa7acrbad90adb6578adabd0dcbf80abad432b76d79b76f687590390b3ff429"
    },
    {
      "name" : "Goku",
      "age" : 22,
      "id_hash" : "0acbfa7acrbad90adb6578adabd0dcbf80abad43276d79b76f687e590390b3ff429"
    }
  ]
}

Having keys named id_hash within an array named ADMIN_LIST, I want to get the value of each instance of id_hash and store it into a string vector std::vector<std::string> Id_Vector = {};. As simple as that.

The number of Admins varies from json files/objects.... as such a hard-coded predetermined number of key values needed would not be accurate.


Rules

  1. The source of the json data can be streamed from a file, file.json file or a file.txt containing json formatted text.
  2. Any json library can be used (as long as it is c++ friendly)
  3. Any json library used, should please come with a link to it's repository or download site.
  4. The use of for loops is very much allowed.
  5. Loop used in determining amount of key values to store should be dynamic.

Sample Code

#include <iostream>
#include "SomeJSON_Library.h"
#include <string>
#include <vector>

int main()
{
   std::vector<std::string> Id_Vector = {};
   for(int g = 0; j <= Length_Of_Keys; j++) // Where Length_Of_Keys refers to the dynamic number of instances within a json file/object
   {
      Id_Vector [j] = FromJson.Array.Keys("id_hash");
   }

   return 0;
}

Such that a call to any id_hash index would hold relative value gotten from Json File.


Further Usage of Json Value Parsing

#include <iostream>
#include "SomeJSON_Library.h"
#include <string>
#include <vector>

int main()
{
   std::vector<std::string> Id_Vector = {};
   std::vector<std::string> Admin_Name = {};

   for(int j = 0; j <= Length_Of_Keys; j++) // Where Length_Of_Keys refers to the dynamic number of instances within a json file/object
   {
      Id_Vector [j] = FromJson.Array.Keys("id_hash"); // Get value of key "id_hash"
      Admin_Name [j] = FromJson.Array.Keys("name"); // Get value of key "name"
   }

   // For the sake of confirming implemented code
   for(int x = 0; x <= Length_Of_Keys; x++) // Length_Of_Keys or sizeof(Id_Vector[0]) / sizeof(Id_Vector)
   {
      std::cout << "Id Hash of Admin " << Admin_Name[x] << "is " << Id_Vector[x] << "\n";
   }

   return 0;
}


Output

Id Hash of Admin Luke is acbfa7acrbad90adb6578adabdff0dcbf80abad43276d79b76f687590390b3ff429

Id Hash of Admin Sasha is 97acbfa7acrbad90adb6578adabd0dcbf80abad43276d79b76f687590390b3ff429

Id Hash of Admin Henry is 2acbfa7acrbad90adb6578adabd0dcbf80abad493276d79b76f687590390b3ff429

Id Hash of Admin Jake is facbfa7acrbad90adb6578adabd0dcbf80abad432b76d79b76f687590390b3ff429

Id Hash of Admin Goku is 0acbfa7acrbad90adb6578adabd0dcbf80abad43276d79b76f687e590390b3ff429



The truth is I'm certain that it's as simple as I've obviously laid it out to be, but I can't for the life of me figure out which Json library or function can do this. I know it's something like FromJsonObj.GetValueFromKey("id_hash"); but I haven't had any luck figuring out how to go about this.

I really wish I knew of a library with such a straight forward syntax call as FromJsonObj.GetValueFromKey();.

I just need an actual C++ code that implements the illustrated desired result.

Help me out and please don't mark as DUPLICATE....Thanks.


Solution

  • I would use a well-known library, like https://github.com/nlohmann/json.

    Here is an example of how you can access array elements by key: accessing elements from nlohmann json