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

How to filter/select member of JsonArray? #1478

Closed
gfox72 opened this issue Jan 23, 2021 · 1 comment
Closed

How to filter/select member of JsonArray? #1478

gfox72 opened this issue Jan 23, 2021 · 1 comment
Labels
question v6 ArduinoJson 6

Comments

@gfox72
Copy link

gfox72 commented Jan 23, 2021

Hi,

Using the assistant for deserializing a JSON string that contains 3 members with identical keys, I get the following output :
const char* balance = elem["balance"]; // "0.00000000", "0.00000001", "0.00000000"

How would I get only the 2nd parameter ? I looked at using a filter for getting only asset BNB (see below) but I can't figure it out using the documentation, doesn't seem to have that feature to only extract only balance value for asset==BNB ?

This is the complete JSON string to deserialize : [{"accountAlias":"mYfREZSgFzAu","asset":"USDT","balance":"0.00000000","crossWalletBalance":"0.00000000","crossUnPnl":"0.00000000","availableBalance":"0.00000000","maxWithdrawAmount":"0.00000000"},{"accountAlias":"mYfREZSgFzAu","asset":"BNB","balance":"0.00000001","crossWalletBalance":"0.00000000","crossUnPnl":"0.00000000","availableBalance":"0.00000000","maxWithdrawAmount":"0.00000001"},{"accountAlias":"mYfREZSgFzAu","asset":"BUSD","balance":"0.00000000","crossWalletBalance":"0.00000000","crossUnPnl":"0.00000000","availableBalance":"0.00000000","maxWithdrawAmount":"0.00000000"}]

And this is the code I'm using (based on assistant):

StaticJsonDocument<768> doc;
deserializeJson(doc, input);
for (JsonObject elem : doc.as()) {
const char* balance = elem["balance"]; // "0.00000000", "0.00000001", "0.00000000"
}
Serial.println(balance);

this gives me:
0.00000000
0.00000001
0.00000000

I need only the balance for asset BNB being 0.00000001

How to ? thanks !

@bblanchon
Copy link
Owner

Hi @gfox72,

It's currently not possible to filter elements of arrays because I couldn't find a satisfying API.
See this older answer for details.
I'm still opened to suggestions.

Best regards,
Benoit

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 7, 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