-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting a flatten json to map<string, string> #1957
Comments
Can you provide an example please? |
Now that I'm thinking about it, maybe it's not that straightforward, but this is what I'm trying to do: // create JSON value
json j_flattened =
{
{"/answer/everything", 42},
{"/happy", true},
{"/list/0", 1},
{"/list/1", 0},
{"/list/2", 2},
{"/name", "Niels"},
{"/nothing", nullptr},
{"/object/currency", "USD"},
{"/object/value", 42.99},
{"/pi", 3.141}
};
map<string, string> flatten_json_to_map; So, I'd like to have the entire |
No, there is no simple way. In particular, the library provides no built-in conversion from boolean/number/null/etc. to string. You could use Idea (untested): map<string, string> flatten_json_to_map(const json& j)
{
map<string, string> result;
auto flattened_j = j.flatten();
for (auto entry : flattened_j.items())
{
switch (entry.value().type())
{
// avoid escaping string value
case value_t::string:
result[entry.key()] = entry.value();
break;
// use dump() for all other value types
default:
result[entry.key()] = entry.value().dump();
break;
}
}
return result;
} |
Your method indeed works and I don't see anything odd, yet. It's what I had in mind too. I'll close the issue and report if I found any bug in it. Thanks! :-) |
I'm reading some on the flatten JSON, and I'm wondering what'd be the best way to cast the whole flatten json to a
map<string, string>
. I'm aware that I can iterate over items, and prepare it manually, but is there any better way?I'm using C++17, and Apple Clang. I'm using the version 3.7.3.
The text was updated successfully, but these errors were encountered: