Skip to content

How to check if a given key exists in a JSON object #1736

@DragonOsman

Description

@DragonOsman
  • Describe what you want to achieve.
    I want to see if a given key exists in a JSON object.

  • Describe what you tried.
    What I've tried is this:

// Performs currency conversion calculation
double calc_result(std::string_view currencykey, std::string_view currency_from, std::string_view currency_to,
	const double money_amount, const double conversion_rate)
{
	using namespace std::chrono_literals;
	using namespace std::string_literals;
	cache_storage cache{ 1h };
	const json sentry = nullptr;
	std::string mapkey{ "currency_list"s };
	json currency_list = cache.query_list(currencykey, mapkey, sentry);

	double result{};
	if (currency_list.contains(std::string(currency_from)) && currency_list.contains(std::string(currency_to)))
	{
		result = money_amount * conversion_rate;
	}
	return result;
}
  • Describe which system (OS, compiler) you are using.
    I'm using Windows 10 Single Language, version 1809 Build 17763.678. VS2017 compiler.

  • Describe which version of the library you are using (release version, develop branch).
    Release version 3.7.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions