Find a key/value in a json object
![Valentin R](https://us.v-cdn.net/6037124/user_avatars/4304535.jpg)
I am wondering if there is a universal way to find an element within an object without knowing the path.
E.g. get the "id" value, without knowing the top level of the object (one/two/three).
{
"one": {
"from": {
"id": 123
}
}}
{
"two": {
"from": {
"id": 123
}
}}
{
"three": {
"from": {
"id": 123
}
}}
What is known and stable is the level where "id" resides
Answers
-
Hi @Valentin R,
An easy way to do this is by using multiple "For each" loops.Each "for each" will get closer to the nested "id".
Since you know that for example "id" will reside to the 3rd level of the path (starting from 1), you will need 3 "For each" loops, the one inside the other.
The only drawback with this is that if your JSON is big, you will need a lot of computation power, and thus the service might be slower.
-
Hey @Lefteris - Buynocodeapps
Not sure I got your suggestion. What function/condition would look for needed key-value level by level?
My current workaround is a bunch of conditionals IF level 1 is one/two/three…, then get the from.id value. But it's obviously not ideal since I had to list all those level 1 variations. -
Hi again @Valentin R,
I am referring to this functionLets say you have this JSON
{
"person": {
"name": "John Doe",
"age": 30,
"details": {
"street": "123 Main St",
}
},
"product": {
"name": "Widget",
"details": {
"manufacturer": "WidgetCo"
}
}
}
If you want to always get the value of "details" (for example), you will just use 2
for each
functions, the one inside the other (considering that you always know the depth of where that specific value resides).
Hope that helps! In case I didn't solve your question, please provide a screenshot of what you have done, since that could possibly be more explanatory. -
@Lefteris - Buynocodeapps
Thanks, I understand your idea now, but Loop doesn't work with json. It requires an array. How do you solve that? -
Hey @Valentin R,
Now I understood what you wanted to do. Please check the series of screenshots for the implementation of the example on your initial post.myID
will always contain the id that you need, regardless of the names of the previous fields, considering that id is on the second nest (like in your example). Similar logic can be implemented for deeper nests.
Let me know if that works for you! -
Nicely done @Lefteris - Buynocodeapps. Appreciate your help.
I never used object functions before, will look into it.
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback