Skip to content
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

Function to resolve a JSON path composed of several keys #1505

Closed
sorokolokarim opened this issue Feb 19, 2021 · 5 comments
Closed

Function to resolve a JSON path composed of several keys #1505

sorokolokarim opened this issue Feb 19, 2021 · 5 comments
Labels
question v6 ArduinoJson 6

Comments

@sorokolokarim
Copy link

sorokolokarim commented Feb 19, 2021

I want to deserialize and get value by this way but it doesn't work.

const char data[] = R"=({"x1":"1","x2":"2"})=";

int8_t value(const char* data, JsonDocument& jsonDoc, JsonVariant key)
{
	deserializeJson(jsonDoc, data);

	return key.as<int8_t>();
}

int8_t x = value(data, jsonDoc, jsonDoc["x2"].to< JsonVariant >());
Serial.print("x : "); Serial.println(x);

// Output x : 1

Question 1 -> Why Output (x) = 1 and not 2
Question 2 -> How to do to have Output (x) = 2

Very very good library, thank you !
Sorry for my english, i speak french

@bblanchon
Copy link
Owner

Hi @sorokolokarim,

It looks like you want something like this:

template<typename T>
T value(const char* json, const char* key)
{
   DynamicJsonDocument doc(4096);
   deserializeJson(doc, json);
   return doc[key].as<T>();
}

const char data[] = R"=({"x1":"1","x2":"2"})=";
int8_t x = value<int8_t>(data, "x2");

This function works with all types except const char*, but you can use String instead.

Best regards,
Benoit

@bblanchon
Copy link
Owner

@sorokolokarim
Copy link
Author

sorokolokarim commented Feb 20, 2021

@bblanchon
Thank for your fast response. But effectively the document is biggest, this is just an exemple.

Question -> Is there a method that can give me the possibility from a function to insert an indefinite number of parameter and that then I will use to retrieve a value or values ​​from the json document?

Example : (something like this)

template<typename T>
T value(const char* json, const char* key, ...)
{
   DynamicJsonDocument doc(4096);
   deserializeJson(doc, json);
   return doc[key][...].as<T>();
}
const char data[] = R"=( {"a": {"b": {"x2": 2}}})=";
expected result 1 : 
int8_t x = value<int8_t>(data, "c", "d"); // 5

expected result 2 : 
int8_t x = value<int8_t>(data, "a", "b", "x2"); // 2

I'm still a beginner and I have a hard time doing something like this.

Thank you very much for the helping hand.
Thank you

@bblanchon
Copy link
Owner

Hi @sorokolokarim,

Doing so require a recursive function with a template parameter pack, like so:

template <typename Key>
JsonVariantConst resolvePath(JsonVariantConst variant, Key key) {
  return variant[key];
}

template <typename Key, typename... Tail>
JsonVariantConst resolvePath(JsonVariantConst variant, Key key, Tail... tail) {
  return resolvePath(variant[key], tail...);
}

template <typename T, typename... Keys>
T value(const char* json, Keys... keys) {
  DynamicJsonDocument doc(4096);
  deserializeJson(doc, json);
  return resolvePath(doc, keys...).template as<T>();
}

const char data[] = R"=( {"a": {"b": {"x2": 2}}})=";
int8_t x = value<int8_t>(data, "a", "b", "x2"); // 2

I know this looks complicated, but it is a common pattern.
BONUS: it works with arrays as well :-)

Demo: https://wandbox.org/permlink/PgICTZ1nIjAULTKO

Best regards,
Benoit

@bblanchon bblanchon changed the title Need help Function to resolve a JSON path composed of several keys Feb 21, 2021
@sorokolokarim
Copy link
Author

Hi @bblanchon 🥇
Thank you very very very much !!! Tu me sauves enormement avec cette solution ! You are a master !! I hope on day get your level or more :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 27, 2021
@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants