c++jsonnlohmann-json

nlohmann/json: how to convert a string to a list and iterate over this list?


nlohmann/json: how to convert a string to a list and iterate over this list?

I am in a very specific case with a list wrapped into a string. See the "Body" and "lidar_calibration" keys below: the associated value is a list embedded into a string "[]".

cat calibration.json

{
    "Head": {
        "ErrorCode": "0",
        "Message": "Success"
    },
    "Body": {
        "lidar_calibration": "[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"
    }
}

cat calibration.cpp

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

int main() {
  {
    std::ifstream inp("calibration.json");
    std::string out; while(inp >> out) std::cout << out; std::cout << std::endl;
  }
  std::cout << std::endl;
  {
    std::ifstream inp("calibration.json");
    nlohmann::json inpJson = nlohmann::json::parse(inp);
    std::cout << inpJson << std::endl;
  }
  std::cout << std::endl;
  {
    std::ifstream inp("calibration.json");
    nlohmann::json inpJson = nlohmann::json::parse(inp);
    std::stringstream str; str << inpJson["Body"]["lidar_calibration"];
    nlohmann::json calibJson = nlohmann::json::parse(str);
    std::cout << calibJson << std::endl;
  }
  std::cout << std::endl;
  {
    std::ifstream inp("calibration.json");
    nlohmann::json inpJson = nlohmann::json::parse(inp);
    std::stringstream str; str << inpJson["Body"]["lidar_calibration"];
    nlohmann::json calibJson = nlohmann::json::parse(str);
    for (auto const & calib : calibJson) std::cout << calib << std::endl;
  }
}
$ g++ -I /usr/include/nlohmann -o calibration calibration.cpp 

$ ./calibration 
{"Head":{"ErrorCode":"0","Message":"Success"},"Body":{"lidar_calibration":"[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"}}

{"Body":{"lidar_calibration":"[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"},"Head":{"ErrorCode":"0","Message":"Success"}}

"[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"

"[{\"i\":1,\"e\":\"15.039\",\"a\":\"0.301\"},{\"i\":2,\"e\":\"14.025\",\"a\":\"0.313\"},{\"i\":3,\"e\":\"13.019\",\"a\":\"0.306\"},{\"i\":4,\"e\":\"12.012\",\"a\":\"0.299\"},{\"i\":5,\"e\":\"11.009\",\"a\":\"0.291\"},{\"i\":6,\"e\":\"10.005\",\"a\":\"0.292\"},{\"i\":7,\"e\":\"9.002\",\"a\":\"0.288\"},{\"i\":8,\"e\":\"8.006\",\"a\":\"0.280\"},{\"i\":9,\"e\":\"7.005\",\"a\":\"0.278\"},{\"i\":10,\"e\":\"6.007\",\"a\":\"0.282\"},{\"i\":11,\"e\":\"5.009\",\"a\":\"0.274\"},{\"i\":12,\"e\":\"4.009\",\"a\":\"0.278\"},{\"i\":13,\"e\":\"3.015\",\"a\":\"0.271\"},{\"i\":14,\"e\":\"2.021\",\"a\":\"0.271\"},{\"i\":15,\"e\":\"1.029\",\"a\":\"0.272\"},{\"i\":16,\"e\":\"0.036\",\"a\":\"0.266\"},{\"i\":17,\"e\":\"-0.957\",\"a\":\"0.262\"},{\"i\":18,\"e\":\"-1.954\",\"a\":\"0.266\"},{\"i\":19,\"e\":\"-2.949\",\"a\":\"0.260\"},{\"i\":20,\"e\":\"-3.945\",\"a\":\"0.254\"},{\"i\":21,\"e\":\"-4.940\",\"a\":\"0.245\"},{\"i\":22,\"e\":\"-5.940\",\"a\":\"0.243\"},{\"i\":23,\"e\":\"-6.937\",\"a\":\"0.243\"},{\"i\":24,\"e\":\"-7.934\",\"a\":\"0.241\"},{\"i\":25,\"e\":\"-8.936\",\"a\":\"0.232\"},{\"i\":26,\"e\":\"-9.936\",\"a\":\"0.238\"},{\"i\":27,\"e\":\"-10.940\",\"a\":\"0.236\"},{\"i\":28,\"e\":\"-11.946\",\"a\":\"0.223\"},{\"i\":29,\"e\":\"-12.950\",\"a\":\"0.215\"},{\"i\":30,\"e\":\"-13.959\",\"a\":\"0.211\"},{\"i\":31,\"e\":\"-14.974\",\"a\":\"0.207\"},{\"i\":32,\"e\":\"-15.992\",\"a\":\"0.205\"}]"

How can I iterate over this list?


Solution

  • Just:

    std::string str = inpJson["Body"]["lidar_calibration"];
    nlohmann::json calibJson = nlohmann::json::parse(str);
    

    Convert it to string instead of serializing with operator<< to another json.