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
3
votes
2 answers

Nlohmann::json& as function argument without including json.hpp in header

I have a class where I want to have a function that uses nlohmann::json& as the argument: class MyClass { public: void foo(nlohmann::json& node); }; But I don't want to include the json.hpp file in my header, just my .cpp. How can I declare…
Chip Burwell
  • 423
  • 3
  • 10
3
votes
2 answers

Json data creation for nlohmann

I have some data like { "GLOBAL DATA": { "FIRST": [ {"BEGIN": "0", "END" : "100"} ], "SECOND":"SomeData", "THIRD":"SomeMoreData" } } I want to add more data to FIRST array. I tried creating the…
Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68
3
votes
2 answers

Array or object: how to use nlohmann::json for simple use cases?

I want to use the nlohmann JSON library in order to read options from a JSON file. Specifying options is optional, as reflected in the constructor in my code example. I'm assuming the JSON structure is an object in its root. Unfortunately, I'm…
Wouter Beek
  • 3,307
  • 16
  • 29
3
votes
1 answer

Calling method nlohmann::json::dump from visual studio intermediate window

I'm trying to dump object from type nlohmann::json into readable string value from visual studio debugger. So in order to invoke the dump method from the nlohmann::json instance, i use visual studio intermediate window. However, it seems like this…
Irad K
  • 867
  • 6
  • 20
3
votes
1 answer

nlohmann's json library convert an array to a vector of structs

Say I have a json array that looks like this: [ { "Name": "test", "Val": "test_val" }, { "Name": "test2", "Val": "test_val2" } ] I want to convert this into a vector of structs: struct Test { string Name; string…
Ank i zle
  • 2,089
  • 3
  • 14
  • 36
3
votes
1 answer

How can I get the array length in nlohmann's json library?

I am using nlohmann/json lib and want to get the length of a json array. Image i have: [ "test", "test2" ] and want to recieve the length (2). When i tried the .length() method, it showed me this error: using json = using json = class…
Ank i zle
  • 2,089
  • 3
  • 14
  • 36
3
votes
0 answers

Unknown breakpoint in nlohmann/json framework

Since some time, when I run my app under Xcode with breakpoints enabled, I hit a unknown breakpoint apparently in a system framework called nlohmann::basic_json: I can continue execution without problems, but this is annoying anyway. Any idea…
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
3
votes
1 answer

nlohmann JSON, change the value of a key

I'm using nlohmann::json and all I need to do is copy a JSON object and then alter some of the keys in it. Is it possible to alter keys in nlohmann::json objects? Essentially what I'm trying to do is the following: json obj1 = {"key with space" :…
Nick Chapman
  • 4,402
  • 1
  • 27
  • 41
2
votes
1 answer

Strange behaviour when using nlohmann-json

I have the following function: // get-function that can handle nullptr. template static const T get(const nlohmann::json& json, const T& retOnNull = T()) { return json.is_null() ? retOnNull : json.get(); } Then I call it like…
DaedalusAlpha
  • 1,610
  • 4
  • 20
  • 33
2
votes
1 answer

Unexpected implicit conversion from nlohmann::json to string

While debugging an application I came up with the following example: #include using nlohmann::json; struct X { X(const std::string& s){} }; int main() { X x("{}"_json); // why compiler allows this? } See at…
2
votes
0 answers

Copy json, but update only keys that exists in target

I have two JSON objects, original and current. The original object was created by reading a configuration file, it have a certain specific keys. The current object was created as a copy of original, then there are keys added to current, as well as…
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2
votes
1 answer

How to convert json to cpp class using nlohmann::json macros?

I am trying to map json data to a class called Game by using the macro NLOHMANN_DEFINE_TYPE_INTRUSIVE but I am getting the error: error: no matching function for call to ‘nlohmann::basic_json<>::get()’ 19 | game…
callum arul
  • 159
  • 6
2
votes
1 answer

Converting json file to json object jumbles up the order of objects

I am trying to parse a json file into a json object using nlohmann json library. This is the json file: { "n":1, "data": { "name":"Chrome", "description":"Browse the internet.", "isEnabled":true } } This is…
callum arul
  • 159
  • 6
2
votes
1 answer

Change Nlohmann json key-value with increment function? c++

Instead of this: j["skills"]["atk"] = j["skills"]["atk"].get() + 6; I want something like this: void json_inc(json ref, int value) //function to increment int key-values { ref = ref.get() + value; } json_inc(j["skills"]["atk"], 6);…
2
votes
2 answers

Convert JSON to array with nlohmann/json

I've not used C++ in over 20 years, so I am a bit rusty on this. I am using the nlohmann/json json.h library and need to convert a JSON string that is an array of objects to an actual array. As an example, I need to take this kind of response from a…
Norm Strassner
  • 225
  • 3
  • 11
1 2
3
16 17