Questions tagged [nlohmann-json]

Use this tag for questions related to nlohmann JSON C++ library

nlohmann JSON is a header-only JSON class for Modern C++ (C++11)

Latest release: 3.9.1 released on 2020-08-06

See the github project

248 questions
38
votes
2 answers

C++: Reading a json object from file with nlohmann json

I am using the nlohmann's json library to work with json objects in c++. Ultimately, I'd like to read a json object from a file, e.g. a simple object like this. { "happy": true, "pi": 3.141 } I'm not quite sure how to approach this. At…
user3515814
  • 957
  • 2
  • 8
  • 13
20
votes
2 answers

C++ debug/print custom type with GDB : the case of nlohmann json library

I'm working on a project using nlohmann's json C++ implementation. How can one easily explore nlohmann's JSON keys/vals in GDB ? I tried to use this STL gdb wrapping since it provides helpers to explore standard C++ library structures that…
LoneWanderer
  • 3,058
  • 1
  • 23
  • 41
16
votes
2 answers

c++ nlohmann json - how to iterate / find a nested object

I am trying to iterate over a nested json, using nlohmann::json. My json object is below: { "one": 1, "two": 2 "three": { "three.one": 3.1 }, } I am trying to iterate and /or find nested objects. But, it seems there is no…
Mopparthy Ravindranath
  • 3,014
  • 6
  • 41
  • 78
14
votes
3 answers

How to check if key present in nested json in c++ using nlohmann

I have a below json data: { "images": [ { "candidates": [ { "confidence": 0.80836, "enrollment_timestamp": "20190613123728", "face_id":…
S Andrew
  • 5,592
  • 27
  • 115
  • 237
14
votes
1 answer

Adding header-only dependencies with CMake

I have a simple project which requires three header-only libraries in order to compile: websocketpp, spdlog and nlohmann/json. The project structure looks like this: └── src ├── app │   ├── CMakeLists.txt │   ├── src │   └── test …
arnaudoff
  • 686
  • 8
  • 20
13
votes
4 answers

How to convert a json object to a map with nlohmann::json?

For example, with nlohmann::json, I can do map> m = { {"a", {1, 2}}, {"b", {2, 3}} }; json j = m; But I cannot do m = j; Any way to convert a json object to a map with nlohmann::json?
user1899020
  • 13,167
  • 21
  • 79
  • 154
12
votes
1 answer

Use nlohmann json to unpack list of integers to a std::vector

I'm using nlohman::json. It's awesome, but is there any way to unpack: { "my_list" : [1,2,3] } into a std:vector ? I can't find any mention in the docs, and std::vector v = j["my_list"]; fails, as does…
P i
  • 29,020
  • 36
  • 159
  • 267
11
votes
1 answer

How do you get a JSON object from a string in nlohmann json?

I have a string that I would like to parse into a json, but _json doesn't seem to work every time. #include #include using nlohmann::json; int main() { // Works as expected json first = "[\"nlohmann\",…
Bob Galbil
  • 113
  • 1
  • 1
  • 4
10
votes
2 answers

Detect with "JSON for Modern C++" library that integer doesn't fit into a specified type?

This code prints -1: #include #include int main() { auto jsonText = "{ \"val\" : 4294967295 }"; auto json = nlohmann::json::parse(jsonText); std::cout << json.at("val").get() << std::endl; } I would…
user10101
  • 1,704
  • 2
  • 20
  • 49
8
votes
1 answer

Template issue with nlohmann::json

Here's my code and the error, and below I'll show some code that works. #include #include #include using JSON = nlohmann::json; using std::cout; using std::endl; using std::string; template
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
8
votes
1 answer

How to iterate over a JSON in JSON for modern c++

I would like to iterate over each of entries in a json object, but I am getting one incomprehensible error after the other. How to correct the following example? #include #include using json = nlohmann::json; void…
mnr
  • 604
  • 2
  • 7
  • 15
7
votes
2 answers

How to iterate over the types of std::variant?

I have some variant using V = std::variant and a function with the prototype V parse(const json&). The function should try to parse all the types (e.g. A, B, then C) till the first success (and it should do it implicitly, for there will be…
Nestor
  • 687
  • 5
  • 12
7
votes
1 answer

Prettify a JSON string in C++ from a .txt file

I'm currently working in C++, getting an HTTP response from a request that I write into a .txt file using ostream. This happens asynchronously and I don't want to change this. Once the data is done being written, I want to read from the…
user7420144
  • 95
  • 1
  • 2
  • 8
6
votes
1 answer

nlohmann json, converting to and from nested structures

I'm using nlohmann json (JSON for modern C++) with an embedded project. The OS is Mongoose OS. Mongoose has a nice config system where config data is handled and laid out in the mos.yml file. This file, at build time, is converted to structures and…
Jim Archer
  • 1,337
  • 5
  • 30
  • 43
6
votes
1 answer

c++, JSON array parsing using nlohmann::json

In a json file imported into my c++ program there is such structure as: { "a":"1", "ec":[ { "a":"0x00", "s":[ {"a":"0xA0"}, {"a":"0xA1"}, ], "b":"v1" }, { …
Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123
1
2 3
16 17