1

I have this JSON:

{
    "San Jose" :  {
        "Moravia" : {
            "San Vicente" : "san_vicente",
            "La Trinidad" : "la_trinidad",
            "Los Colegios" : "los_colegios"
        },
        "Desamparados" : {
            "San Miguel" : "san_miguel",
            "Gravilias" : "gravilias",
            "Damas" : "damas"
        },
        "Curridabat" : {
            "Granadilla" : "granadilla",
            "Sanchez" : "sanchez",
            "Tirrases" : "tirrases"
        }
    },
    "Cartago" : {
        "Oreamuno" : {
            "Cot" : "cot",
            "Cipreses" : "cipreses",
            "Santa Rosa" : "santa_rosa"
        },
        "La Union" : {
            "Tres Rios" : "tres_rios",
            "Dulce Nombre" : "dulce_nombre",
            "Concepcion" : "concepcion"
        },
        "Turrialba" : {
            "La Suiza" : "la_suiza",
            "Peralta" : "peralta",
            "La Isabel" : "la_isabel"
        }
    },
    "Puntarenas" : {
        "Golfito" : {
            "Puerto Jimenez" : "puerto_jimenez",
            "Guaycara" : "guaycara",
            "Pavon" : "pavon"
        },
        "Coto Brus" : {
            "San Vito" : "san_vito",
            "Limoncito" : "limoncito",
            "Agua Buena" : "agua_buena"
        },
        "Corredores" : {
            "La Cuesta" : "la_cuesta",
            "Paso Canoas" : "paso_canoas",
            "Laurel" : "laurel"
        }
    }
}

And I want to get a certain value and his corresponding parent/child. For example:

var value = json.get("Cot");
var child =  value.child();

Or:

var value = json.get("Cot");
var parent =  value.parent();

Where var value = json.get("Cot"); is a reference to this value in the JSON object and value.child();/value.parent(); is a reference to his corresponding child(children)/parent(s).

Note: This must work no matter the size of the JSON and his dimensions. For example:

{
    "val1" :  {
        "val2" : {
            "val3" : { 
                       "valX" 
                     }
                 }
              }
}
Dreemurr23
  • 13
  • 2
  • Does this answer your question? [access parent object in javascript](https://stackoverflow.com/questions/1789892/access-parent-object-in-javascript) – Mister Jojo Oct 17 '20 at 17:09
  • https://stackoverflow.com/questions/53543303/find-a-full-object-path-to-a-given-value-with-javascript – Robert Oct 17 '20 at 17:16
  • Thanks, seems to solve my problem. I will be on testing and give you a definitive answer if it works. – Dreemurr23 Oct 17 '20 at 17:40
  • `"Cot"` is the key and `"cot"` is the value. You are searching for key or value? – adiga Oct 17 '20 at 17:47
  • Tecnically both of them, I need to match a string in the JSON no matter its level or if is a key or a value, just need a perfect match with the given string. – Dreemurr23 Oct 17 '20 at 20:11

1 Answers1

0

Here is what I used after scouring everyone else's work:

function find(obj, item, value) {   
    function objFind(obj, item, value){
        for(var key in obj) {                                  

           if(obj[key] && obj[key].length ==undefined) {     
                if(obj[item]==value){
                    return obj;
                }
              objFind(obj[key], item, value);             
                
            }else{ 
                if(obj[key] && obj[key].length >0){
                    if(Array.isArray(obj[key])){
                        for(var i=0;i<obj[key].length;i++){
                            var results = objFind(obj[key][i], item, value);
                            if(typeof results === "object"){
                                return results;
                            }
                        }
                    }
                }
            }
        }
    }
    if(obj.length >0){
        for(var i=0;i<obj.length;i++){
            var results=objFind(obj[i], item, value);
            if(typeof results === "object"){
                return results;
            }
        }
    }
    else{
       var results=objFind(obj, item, value);
        if(typeof results === "object"){
            return results;
        }
    }
}

Then to use it:

var Target = find(jsonData,"id", 5);
Alan
  • 2,046
  • 2
  • 20
  • 43