Find a key/value in a json object

Options
Valentin R
Valentin R Member
edited August 2023 in ? Help! I'm a Noob

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



Tagged:

Answers

  • Lefteris - blupry.com
    Options

    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.

  • Valentin R
    Valentin R Member
    Options

    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.

  • Lefteris - blupry.com
    Lefteris - blupry.com Member ✭✭
    edited August 2023
    Options

    Hi again @Valentin R,

    I am referring to this function

    Lets 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.

  • Valentin R
    Valentin R Member
    Options

    @Lefteris - Buynocodeapps
    Thanks, I understand your idea now, but Loop doesn't work with json. It requires an array. How do you solve that?

  • Lefteris - blupry.com
    Options

    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!

  • Valentin R
    Valentin R Member
    Options

    Nicely done @Lefteris - Buynocodeapps. Appreciate your help.
    I never used object functions before, will look into it.