skip to Main Content

I am trying to get specific value from deeply nested stringified JSON.

Below is the example

const arr = [
    {
        "type": "exception-handler",
        "action": "try-catch",
        "extra": {
            "id": "aefwwefefwARG@.[t.2"
        },
        "try": [
            {
                "type": "unit",
                "action": "create",
                "request": [
                    {
                        "type": "operator",
                        "action": "request",
                        "extra": {
                            "id": "%m6nwefewfqAuo!@"
                        },
                        "id": "58282ac47ccc45b79c6bc",
                        "method": {
                            "type": "miscellaneous",
                            "action": "protocol-method",
                            "extra": {
                                "id": "H+JewwefeTgHv"
                            },
                            "method": "post"
                        },
                        "end-point": {
                            "type": "protocol-end-point",
                            "action": "url",
                            "extra": {
                                "id": "=wefwefqe"
                            },
                            "end-point": {
                                "type": "primitive",
                                "action": "string",
                                "extra": {
                                    "id": "wefwefewfewd!"
                                },
                                "value": "http://localhost:8081/plan/entrance"
                            }
                        },
                        "header": {
                            "type": "hashmap",
                            "action": "create",
                            "extra": {
                                "id": "[wvsdvsdvsdvY"
                            },
                            "values": [
                                {
                                    "type": "parameter",
                                    "action": "create",
                                    "extra": {
                                        "id": "sdfwefwef"
                                    },
                                    "key": {
                                        "type": "primitive",
                                        "action": "string",
                                        "extra": {
                                            "id": "sdfvwvwv_-|z*"
                                        },
                                        "value": "Content-Type"
                                    },
                                    "value": {
                                        "type": "miscellaneous",
                                        "action": "protocol-content-type",
                                        "content-type": "application/json"
                                    },
                                    "description": {
                                        "type": "primitive",
                                        "action": "string",
                                        "extra": {
                                            "id": "4xW$1)uu^o5Qyd*:_%b-"
                                        },
                                        "value": "Content-Type"
                                    },
                                    "datatype": {
                                        "type": "miscellaneous",
                                        "action": "parameter-type",
                                        "extra": {
                                            "id": "0q;~b_SF4bUu|y`I~5@W"
                                        },
                                        "parameter-type": "string"
                                    },
                                    "required": {
                                        "type": "primitive",
                                        "action": "boolean",
                                        "extra": {
                                            "id": "pddddvwvewerr"
                                        },
                                        "value": true
                                    }
                                }
                            ]
                        },
                        "body": {
                            "type": "hashmap",
                            "action": "create",
                            "extra": {
                                "id": "wecwcecweqweq"
                            },
                            "values": []
                        }
                    }
                ],
                "response": [
                    {
                        "type": "operator",
                        "action": "response",
                        "extra": {
                            "id": "u^ewfvwKlbu"
                        },
                        "id": "d2a26b2943c12fc9c493cb152773c1ad",
                        "context": [
                            {
                                "type": "response-context",
                                "action": "create",
                                "extra": {
                                    "id": "/wfefwf]JbM"
                                },
                                "status_code": {
                                    "type": "primitive",
                                    "action": "integer",
                                    "extra": {
                                        "id": "bYhweqwdqwVZs:~"
                                    },
                                    "value": 200
                                },
                                "header": {
                                    "type": "hashmap",
                                    "action": "create",
                                    "extra": {
                                        "id": "$0{7z~p%Yp.{`SMtV!o#"
                                    },
                                    "values": [
                                        {
                                            "type": "parameter",
                                            "action": "create",
                                            "extra": {
                                                "id": "efwefwB"
                                            },
                                            "key": {
                                                "type": "primitive",
                                                "action": "string",
                                                "extra": {
                                                    "id": "#9.Rwefvwvfs"
                                                },
                                                "value": "Content-Type"
                                            },
                                            "value": {
                                                "type": "miscellaneous",
                                                "action": "protocol-content-type",
                                                "content-type": "application/json"
                                            },
                                            "description": {
                                                "type": "primitive",
                                                "action": "strwffwe",
                                                "extra": {
                                                    "id": "I=jUZ6Xc-u}UfqHqe8T6"
                                                },
                                                "value": "Content-Type"
                                            },
                                            "datatype": {
                                                "type": "miscellaneous",
                                                "action": "parameter-type",
                                                "extra": {
                                                    "id": "B*(qy~(zjjjI-,u*aN-."
                                                },
                                                "parameter-type": "string"
                                            },
                                            "required": {
                                                "type": "primitive",
                                                "action": "boolean",
                                                "extra": {
                                                    "id": "!_4.P~5A#DR(.@j#tee_"
                                                },
                                                "value": true
                                            }
                                        }
                                    ]
                                },
                                "body": {
                                    "type": "hashmap",
                                    "action": "create",
                                    "extra": {
                                        "id": "2xlF!I@063F+ag4%AGGn"
                                    },
                                    "values": [
                                        {
                                            "type": "parameter",
                                            "action": "create",
                                            "extra": {
                                                "id": "QNc=;UPk5m`g?d+p$F;)"
                                            },
                                            "key": {
                                                "type": "primitive",
                                                "action": "string",
                                                "extra": {
                                                    "id": "fX7,hQW_SvG-HI_Kbvc,"
                                                },
                                                "value": "result"
                                            },
                                            "value": {
                                                "type": "variable",
                                                "action": "get",
                                                "extra": {
                                                    "id": "_41Rlcb++on)q=sb,i$e"
                                                },
                                                "name": {
                                                    "type": "primitive",
                                                    "action": "string",
                                                    "extra": {
                                                        "id": "31AhsBljlkYm=9aLy+Wt"
                                                    },
                                                    "value": "qqq1"
                                                }
                                            },
                                            "description": {
                                                "type": "primitive",
                                                "action": "string",
                                                "extra": {
                                                    "id": "Si$=Ebxj{Yyt2yI;s@:e"
                                                },
                                                "value": "result"
                                            },
                                            "datatype": {
                                                "type": "miscellaneous",
                                                "action": "parameter-type",
                                                "extra": {
                                                    "id": "L3Z?PB8k6)D]5##_U*63"
                                                },
                                                "parameter-type": "string"
                                            },
                                            "required": {
                                                "type": "primitive",
                                                "action": "boolean",
                                                "extra": {
                                                    "id": "=3QlBlyN%72a#E{!4ovC"
                                                },
                                                "value": false
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                ],
                "statements": [
                    {
                        "type": "helper",
                        "action": "code-section",
                        "extra": {
                            "id": ")$x]%MY;vXFM66t$[#3g"
                        },
                        "statements": [
                            {
                                "type": "document",
                                "action": "comment-note",
                                "extra": {
                                    "id": "8prbh]Hq?^s2BV=`J4|$"
                                },
                                "comment": {
                                    "type": "primitive",
                                    "action": "string",
                                    "value": "code 1"
                                }
                            },
                            {
                                "type": "variable",
                                "action": "create",
                                "extra": {
                                    "id": "QubE)kG{r7H9xR-}CHF7"
                                },
                                "name": {
                                    "type": "primitive",
                                    "action": "string",
                                    "extra": {
                                        "id": "h=rET1COg7/YoxR{OfYz"
                                    },
                                    "value": "qqq2"
                                }
                            },
                            {
                                "type": "variable",
                                "action": "set",
                                "extra": {
                                    "id": "p[gSm|F$@CU3M_F!Tl!o"
                                },
                                "name": {
                                    "type": "primitive",
                                    "action": "string",
                                    "extra": {
                                        "id": "Y9)#ccnXbtpN}[zq!U!a"
                                    },
                                    "value": "qqq2"
                                },
                                "value": {
                                    "type": "pair",
                                    "action": "create",
                                    "extra": {
                                        "id": "b=/HL|O--7^=~{c4]SYk"
                                    },
                                    "key": {
                                        "type": "primitive",
                                        "action": "string",
                                        "extra": {
                                            "id": "2,aMj@kiv$aG]2Fg(X+F"
                                        },
                                        "value": "qqq"
                                    },
                                    "value": {
                                        "type": "pair",
                                        "action": "create",
                                        "extra": {
                                            "id": "_]kqE^cuy#mK!s5~KufR"
                                        },
                                        "key": {
                                            "type": "primitive",
                                            "action": "string",
                                            "extra": {
                                                "id": "u9v[T7T4t]oRB)KMKAo7"
                                            },
                                            "value": "qqq"
                                        },
                                        "value": {
                                            "type": "primitive",
                                            "action": "string",
                                            "extra": {
                                                "id": "/,V=$)=-Ex*1KAQpKim7"
                                            },
                                            "value": "1234"
                                        }
                                    }
                                }
                            }
                        ]
                    },
                    {
                        "type": "helper",
                        "action": "code-section",
                        "extra": {
                            "id": "Nwefwvwwe|T;2"
                        },
                        "statements": [
                            {
                                "type": "document",
                                "action": "comment-note",
                                "extra": {
                                    "id": "!hba3~wcwe+!AjcF"
                                },
                                "comment": {
                                    "type": "primitive",
                                    "action": "string",
                                    "value": "code 2"
                                }
                            },
                            {
                                "type": "variable",
                                "action": "create",
                                "extra": {
                                    "id": "`.zqkDw?Qd$BD#;U`pb7"
                                },
                                "name": {
                                    "type": "primitive",
                                    "action": "string",
                                    "extra": {
                                        "id": "awefwvevK~0Y"
                                    },
                                    "value": "qqq1"
                                }
                            },
                            {
                                "type": "variable",
                                "action": "set",
                                "extra": {
                                    "id": "S,wVwvvefdwefFx/"
                                },
                                "name": {
                                    "type": "primitive",
                                    "action": "string",
                                    "extra": {
                                        "id": ":QwevdsdvdWex"
                                    },
                                    "value": "qqq1"
                                },
                                "value": {
                                    "type": "custom-util",
                                    "action": "create",
                                    "extra": {
                                        "id": "xLwedwvwev|K"
                                    },
                                    "arguments": [
                                        {
                                            "type": "pair",
                                            "action": "create",
                                            "extra": {
                                                "id": "ig-vsdvsdO0O80D!"
                                            },
                                            "key": {
                                                "type": "primitive",
                                                "action": "string",
                                                "extra": {
                                                    "id": "AJOawef`hZ|d,"
                                                },
                                                "value": "arg1"
                                            },
                                            "value": {
                                                "type": "primitive",
                                                "action": "null",
                                                "extra": {
                                                    "id": "8MraKndfsdf;$MF4Cb"
                                                }
                                            }
                                        },
                                        {
                                            "type": "pair",
                                            "action": "create",
                                            "extra": {
                                                "id": "`eeGi4|7oGaRT7UB;AvP"
                                            },
                                            "key": {
                                                "type": "primitive",
                                                "action": "string",
                                                "extra": {
                                                    "id": "vewdqwd4X"
                                                },
                                                "value": "arg2"
                                            },
                                            "value": {
                                                "type": "primitive",
                                                "action": "null",
                                                "extra": {
                                                    "id": "Xcswefsddv,"
                                                }
                                            }
                                        }
                                    ],
                                    "payload": [],
                                    "return-value": {
                                        "type": "primitive",
                                        "action": "string",
                                        "extra": {
                                            "id": "eodx(7Oz7)7Gw;.dF4kt"
                                        },
                                        "value": "retData"
                                    },
                                    "property": {
                                        "type": "property",
                                        "action": "custom-util",
                                        "extra": [],
                                        "id": {
                                            "type": "primitive",
                                            "action": "string",
                                            "extra": [],
                                            "value": "3e1589wewefewfq2332cc2a83"
                                        },
                                        "name": {
                                            "type": "primitive",
                                            "action": "string",
                                            "extra": [],
                                            "value": "test1"
                                        },
                                        "description": {
                                            "type": "primitive",
                                            "action": "string",
                                            "extra": [],
                                            "value": ""
                                        },
                                        "created-date": {
                                            "type": "primitive",
                                            "action": "string",
                                            "extra": [],
                                            "value": ""
                                        },
                                        "updated-date": {
                                            "type": "primitive",
                                            "action": "string",
                                            "extra": [],
                                            "value": ""
                                        }
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        ],
        "catchers": [
            {
                "type": "exception-handler",
                "action": "exception-catcher",
                "extra": {
                    "id": "6ZCfwefwfewfO)f}UF!"
                },
                "id": "064673gerg081c4e1",
                "exceptions": [
                    {
                        "type": "miscellaneous",
                        "action": "exception-type",
                        "extra": {
                            "id": "IQRLF7Shgd46:T!UXA~V"
                        },
                        "exception-type": "RuntimeException"
                    }
                ],
                "statements": []
            }
        ]
    }
];

This is deeply nested array with object and it could be nested deeper. And I want to get string ids inside extra object.

"extra": {
   "id": "aefwwefefwARG@.[t.2"
 },

And make them as a set of array like below.

const ids = ["aefwwefefwARG@.[t.2", "%m6nwefewfqAuo!@", "H+JewwefeTgHv", ...]

I used JSON.stringify method to flatten the array. However, I forgot when I use the method, the array is converted to a string. Therefore I am not able to get the specific object with key name extra.

Is there way I can get the value of extra from stringified array?

3

Answers


  1. You can do this using a recursive function, let’s call it getAllPropertyValues().

    We loop over each key in the input object obj, if the value at that key is an object (or array) we call getAllPropertyValues on the value, if not we’ll add the value to the result array if it matches the desired property.

    You could use a Set() if you wished to get only unique ids rather than each and every one.

    We’ll match only the desired extra objects by passing in a parent value and testing against each object’s parent.

    const arr = [ { "type": "exception-handler", "action": "try-catch", "extra": { "id": "aefwwefefwARG@.[t.2" }, "try": [ { "type": "unit", "action": "create", "request": [ { "type": "operator", "action": "request", "extra": { "id": "%m6nwefewfqAuo!@" }, "id": "58282ac47ccc45b79c6bc", "method": { "type": "miscellaneous", "action": "protocol-method", "extra": { "id": "H+JewwefeTgHv" }, "method": "post" }, "end-point": { "type": "protocol-end-point", "action": "url", "extra": { "id": "=wefwefqe" }, "end-point": { "type": "primitive", "action": "string", "extra": { "id": "wefwefewfewd!" }, "value": "http://localhost:8081/plan/entrance" } }, "header": { "type": "hashmap", "action": "create", "extra": { "id": "[wvsdvsdvsdvY" }, "values": [ { "type": "parameter", "action": "create", "extra": { "id": "sdfwefwef" }, "key": { "type": "primitive", "action": "string", "extra": { "id": "sdfvwvwv_-|z*" }, "value": "Content-Type" }, "value": { "type": "miscellaneous", "action": "protocol-content-type", "content-type": "application/json" }, "description": { "type": "primitive", "action": "string", "extra": { "id": "4xW$1)uu^o5Qyd*:_%b-" }, "value": "Content-Type" }, "datatype": { "type": "miscellaneous", "action": "parameter-type", "extra": { "id": "0q;~b_SF4bUu|y`I~5@W" }, "parameter-type": "string" }, "required": { "type": "primitive", "action": "boolean", "extra": { "id": "pddddvwvewerr" }, "value": true } } ] }, "body": { "type": "hashmap", "action": "create", "extra": { "id": "wecwcecweqweq" }, "values": [] } } ], "response": [ { "type": "operator", "action": "response", "extra": { "id": "u^ewfvwKlbu" }, "id": "d2a26b2943c12fc9c493cb152773c1ad", "context": [ { "type": "response-context", "action": "create", "extra": { "id": "/wfefwf]JbM" }, "status_code": { "type": "primitive", "action": "integer", "extra": { "id": "bYhweqwdqwVZs:~" }, "value": 200 }, "header": { "type": "hashmap", "action": "create", "extra": { "id": "$0{7z~p%Yp.{`SMtV!o#" }, "values": [ { "type": "parameter", "action": "create", "extra": { "id": "efwefwB" }, "key": { "type": "primitive", "action": "string", "extra": { "id": "#9.Rwefvwvfs" }, "value": "Content-Type" }, "value": { "type": "miscellaneous", "action": "protocol-content-type", "content-type": "application/json" }, "description": { "type": "primitive", "action": "strwffwe", "extra": { "id": "I=jUZ6Xc-u}UfqHqe8T6" }, "value": "Content-Type" }, "datatype": { "type": "miscellaneous", "action": "parameter-type", "extra": { "id": "B*(qy~(zjjjI-,u*aN-." }, "parameter-type": "string" }, "required": { "type": "primitive", "action": "boolean", "extra": { "id": "!_4.P~5A#DR(.@j#tee_" }, "value": true } } ] }, "body": { "type": "hashmap", "action": "create", "extra": { "id": "2xlF!I@063F+ag4%AGGn" }, "values": [ { "type": "parameter", "action": "create", "extra": { "id": "QNc=;UPk5m`g?d+p$F;)" }, "key": { "type": "primitive", "action": "string", "extra": { "id": "fX7,hQW_SvG-HI_Kbvc," }, "value": "result" }, "value": { "type": "variable", "action": "get", "extra": { "id": "_41Rlcb++on)q=sb,i$e" }, "name": { "type": "primitive", "action": "string", "extra": { "id": "31AhsBljlkYm=9aLy+Wt" }, "value": "qqq1" } }, "description": { "type": "primitive", "action": "string", "extra": { "id": "Si$=Ebxj{Yyt2yI;s@:e" }, "value": "result" }, "datatype": { "type": "miscellaneous", "action": "parameter-type", "extra": { "id": "L3Z?PB8k6)D]5##_U*63" }, "parameter-type": "string" }, "required": { "type": "primitive", "action": "boolean", "extra": { "id": "=3QlBlyN%72a#E{!4ovC" }, "value": false } } ] } } ] } ], "statements": [ { "type": "helper", "action": "code-section", "extra": { "id": ")$x]%MY;vXFM66t$[#3g" }, "statements": [ { "type": "document", "action": "comment-note", "extra": { "id": "8prbh]Hq?^s2BV=`J4|$" }, "comment": { "type": "primitive", "action": "string", "value": "code 1" } }, { "type": "variable", "action": "create", "extra": { "id": "QubE)kG{r7H9xR-}CHF7" }, "name": { "type": "primitive", "action": "string", "extra": { "id": "h=rET1COg7/YoxR{OfYz" }, "value": "qqq2" } }, { "type": "variable", "action": "set", "extra": { "id": "p[gSm|F$@CU3M_F!Tl!o" }, "name": { "type": "primitive", "action": "string", "extra": { "id": "Y9)#ccnXbtpN}[zq!U!a" }, "value": "qqq2" }, "value": { "type": "pair", "action": "create", "extra": { "id": "b=/HL|O--7^=~{c4]SYk" }, "key": { "type": "primitive", "action": "string", "extra": { "id": "2,aMj@kiv$aG]2Fg(X+F" }, "value": "qqq" }, "value": { "type": "pair", "action": "create", "extra": { "id": "_]kqE^cuy#mK!s5~KufR" }, "key": { "type": "primitive", "action": "string", "extra": { "id": "u9v[T7T4t]oRB)KMKAo7" }, "value": "qqq" }, "value": { "type": "primitive", "action": "string", "extra": { "id": "/,V=$)=-Ex*1KAQpKim7" }, "value": "1234" } } } } ] }, { "type": "helper", "action": "code-section", "extra": { "id": "Nwefwvwwe|T;2" }, "statements": [ { "type": "document", "action": "comment-note", "extra": { "id": "!hba3~wcwe+!AjcF" }, "comment": { "type": "primitive", "action": "string", "value": "code 2" } }, { "type": "variable", "action": "create", "extra": { "id": "`.zqkDw?Qd$BD#;U`pb7" }, "name": { "type": "primitive", "action": "string", "extra": { "id": "awefwvevK~0Y" }, "value": "qqq1" } }, { "type": "variable", "action": "set", "extra": { "id": "S,wVwvvefdwefFx/" }, "name": { "type": "primitive", "action": "string", "extra": { "id": ":QwevdsdvdWex" }, "value": "qqq1" }, "value": { "type": "custom-util", "action": "create", "extra": { "id": "xLwedwvwev|K" }, "arguments": [ { "type": "pair", "action": "create", "extra": { "id": "ig-vsdvsdO0O80D!" }, "key": { "type": "primitive", "action": "string", "extra": { "id": "AJOawef`hZ|d," }, "value": "arg1" }, "value": { "type": "primitive", "action": "null", "extra": { "id": "8MraKndfsdf;$MF4Cb" } } }, { "type": "pair", "action": "create", "extra": { "id": "`eeGi4|7oGaRT7UB;AvP" }, "key": { "type": "primitive", "action": "string", "extra": { "id": "vewdqwd4X" }, "value": "arg2" }, "value": { "type": "primitive", "action": "null", "extra": { "id": "Xcswefsddv," } } } ], "payload": [], "return-value": { "type": "primitive", "action": "string", "extra": { "id": "eodx(7Oz7)7Gw;.dF4kt" }, "value": "retData" }, "property": { "type": "property", "action": "custom-util", "extra": [], "id": { "type": "primitive", "action": "string", "extra": [], "value": "3e1589wewefewfq2332cc2a83" }, "name": { "type": "primitive", "action": "string", "extra": [], "value": "test1" }, "description": { "type": "primitive", "action": "string", "extra": [], "value": "" }, "created-date": { "type": "primitive", "action": "string", "extra": [], "value": "" }, "updated-date": { "type": "primitive", "action": "string", "extra": [], "value": "" } } } } ] } ] } ], "catchers": [ { "type": "exception-handler", "action": "exception-catcher", "extra": { "id": "6ZCfwefwfewfO)f}UF!" }, "id": "064673gerg081c4e1", "exceptions": [ { "type": "miscellaneous", "action": "exception-type", "extra": { "id": "IQRLF7Shgd46:T!UXA~V" }, "exception-type": "RuntimeException" } ], "statements": [] } ] } ];
    
    function getAllPropertyValues(obj, property, parent, parentKey = '') {
        let values = [];
        for(let k in obj) {
            if (obj[k] && typeof(obj[k]) === 'object') {
                values = [...values, ...getAllPropertyValues(obj[k], property, parent, k)]
            } else if ((k === property) && (parent === parentKey)) { 
                values.push(obj[k])
            }
        }
        return values;
    }
    
    console.log('Id array:', getAllPropertyValues(arr, 'id', 'extra'))
    .as-console-wrapper { max-height: 100% !important; }
    Login or Signup to reply.
  2. You probably don’t need to JSON.stringify.
    If your json structure is in an object json.
    You can try something like:

    
    function findKeyValuesInJson(json, needle_key, nested_needle) {
       let found_values = [];
       let objectsToCheck = [json];
       while(objectsToCheck.length > 0) {
          let obj = objectsToCheck.pop();
          let keysToCheck = Object.keys(obj);
          while(keysToCheck.length > 0) {
             let key = keysToCheck.pop();
             if (key === needle_key) {
                found_values.push(obj[key])
                if(nested_needle && typeof obj[key] === 'object' && obj[key] !== null) {
                   objectsToCheck.push(obj[key]);
                }
             } else if (typeof obj[key] === 'object' && obj[key] !== null) {
                objectsToCheck.push(obj[key]);
             }
          }
       }
       return found_values;
    }
    function findExtraInJson(json) {
       return findKeyValuesInJson(json, 'extra');
    }
    const extraProps = findExtraInJson(json);
    

    It basically iterates through the entire object and collects the
    values of key extra in an array.

    By default, we’re not checking the key extra again for the key extra. You can enable that by passing true as the third argument to findKeyValuesInJson or you can put the if statement with objectsToCheck(obj[key]) outside the else-if statement so that all keys will go through with this check.

    After getting the property values in an array, you can filter/map it and get what you need like this:

    extraProps
        .filter(extra => extra.id !== undefined)
        .map(extra => extra.id);
    
    
    Login or Signup to reply.
  3. Just another approach recursively visiting an array passed as input and returning an array of values of all the .extra.id property values found in its nested objects.

    As a side note, please forget what you think to know about json. That’s a formal syntax to serialize javascript objects. You actually have a javascript object definition there and that’s all we need here. JSON.stringify would be needed to serialize the object but it’s not our approach here.

    This is the main logics:

    //visits the arr array recursively looking for objects having .extra.id
    //that, if found, will be pushed in the accumulator
    function grabExtraIds(arr, accumulator = []) {
      //for each item in the array
      arr.forEach(nestedObject => {
    
        //pushes the .extra.id value of this nestedObject if existent
        const extraId = nestedObject ? .extra ? .id;
        if (extraId !== undefined)
          accumulator.push(extraId);
    
        //for each property of the current nestedObject
        Object.getOwnPropertyNames(nestedObject)
          .forEach(propertyName => {
            const property = nestedObject[propertyName];
            //if the property value is of type array
            if (Array.isArray(property))
              //recursively call the grabExtraIds with this object
              grabExtraIds(property, accumulator);
          });
      });
    }  
    
      
    

    This is the found ids:

    [
      "aefwwefefwARG@.[t.2",
      "%m6nwefewfqAuo!@",
      "u^ewfvwKlbu",
      "/wfefwf]JbM",
      ")$x]%MY;vXFM66t$[#3g",
      "8prbh]Hq?^s2BV=`J4|$",
      "QubE)kG{r7H9xR-}CHF7",
      "p[gSm|F$@CU3M_F!Tl!o",
      "Nwefwvwwe|T;2",
      "!hba3~wcwe+!AjcF",
      "`.zqkDw?Qd$BD#;U`pb7",
      "S,wVwvvefdwefFx/",
      "6ZCfwefwfewfO)f}UF!",
      "IQRLF7Shgd46:T!UXA~V"
    ]
    

    And this is the live demo:

    const arr = [{
      "type": "exception-handler",
      "action": "try-catch",
      "extra": {
        "id": "aefwwefefwARG@.[t.2"
      },
      "try": [{
        "type": "unit",
        "action": "create",
        "request": [{
          "type": "operator",
          "action": "request",
          "extra": {
            "id": "%m6nwefewfqAuo!@"
          },
          "id": "58282ac47ccc45b79c6bc",
          "method": {
            "type": "miscellaneous",
            "action": "protocol-method",
            "extra": {
              "id": "H+JewwefeTgHv"
            },
            "method": "post"
          },
          "end-point": {
            "type": "protocol-end-point",
            "action": "url",
            "extra": {
              "id": "=wefwefqe"
            },
            "end-point": {
              "type": "primitive",
              "action": "string",
              "extra": {
                "id": "wefwefewfewd!"
              },
              "value": "http://localhost:8081/plan/entrance"
            }
          },
          "header": {
            "type": "hashmap",
            "action": "create",
            "extra": {
              "id": "[wvsdvsdvsdvY"
            },
            "values": [{
              "type": "parameter",
              "action": "create",
              "extra": {
                "id": "sdfwefwef"
              },
              "key": {
                "type": "primitive",
                "action": "string",
                "extra": {
                  "id": "sdfvwvwv_-|z*"
                },
                "value": "Content-Type"
              },
              "value": {
                "type": "miscellaneous",
                "action": "protocol-content-type",
                "content-type": "application/json"
              },
              "description": {
                "type": "primitive",
                "action": "string",
                "extra": {
                  "id": "4xW$1)uu^o5Qyd*:_%b-"
                },
                "value": "Content-Type"
              },
              "datatype": {
                "type": "miscellaneous",
                "action": "parameter-type",
                "extra": {
                  "id": "0q;~b_SF4bUu|y`I~5@W"
                },
                "parameter-type": "string"
              },
              "required": {
                "type": "primitive",
                "action": "boolean",
                "extra": {
                  "id": "pddddvwvewerr"
                },
                "value": true
              }
            }]
          },
          "body": {
            "type": "hashmap",
            "action": "create",
            "extra": {
              "id": "wecwcecweqweq"
            },
            "values": []
          }
        }],
        "response": [{
          "type": "operator",
          "action": "response",
          "extra": {
            "id": "u^ewfvwKlbu"
          },
          "id": "d2a26b2943c12fc9c493cb152773c1ad",
          "context": [{
            "type": "response-context",
            "action": "create",
            "extra": {
              "id": "/wfefwf]JbM"
            },
            "status_code": {
              "type": "primitive",
              "action": "integer",
              "extra": {
                "id": "bYhweqwdqwVZs:~"
              },
              "value": 200
            },
            "header": {
              "type": "hashmap",
              "action": "create",
              "extra": {
                "id": "$0{7z~p%Yp.{`SMtV!o#"
              },
              "values": [{
                "type": "parameter",
                "action": "create",
                "extra": {
                  "id": "efwefwB"
                },
                "key": {
                  "type": "primitive",
                  "action": "string",
                  "extra": {
                    "id": "#9.Rwefvwvfs"
                  },
                  "value": "Content-Type"
                },
                "value": {
                  "type": "miscellaneous",
                  "action": "protocol-content-type",
                  "content-type": "application/json"
                },
                "description": {
                  "type": "primitive",
                  "action": "strwffwe",
                  "extra": {
                    "id": "I=jUZ6Xc-u}UfqHqe8T6"
                  },
                  "value": "Content-Type"
                },
                "datatype": {
                  "type": "miscellaneous",
                  "action": "parameter-type",
                  "extra": {
                    "id": "B*(qy~(zjjjI-,u*aN-."
                  },
                  "parameter-type": "string"
                },
                "required": {
                  "type": "primitive",
                  "action": "boolean",
                  "extra": {
                    "id": "!_4.P~5A#DR(.@j#tee_"
                  },
                  "value": true
                }
              }]
            },
            "body": {
              "type": "hashmap",
              "action": "create",
              "extra": {
                "id": "2xlF!I@063F+ag4%AGGn"
              },
              "values": [{
                "type": "parameter",
                "action": "create",
                "extra": {
                  "id": "QNc=;UPk5m`g?d+p$F;)"
                },
                "key": {
                  "type": "primitive",
                  "action": "string",
                  "extra": {
                    "id": "fX7,hQW_SvG-HI_Kbvc,"
                  },
                  "value": "result"
                },
                "value": {
                  "type": "variable",
                  "action": "get",
                  "extra": {
                    "id": "_41Rlcb++on)q=sb,i$e"
                  },
                  "name": {
                    "type": "primitive",
                    "action": "string",
                    "extra": {
                      "id": "31AhsBljlkYm=9aLy+Wt"
                    },
                    "value": "qqq1"
                  }
                },
                "description": {
                  "type": "primitive",
                  "action": "string",
                  "extra": {
                    "id": "Si$=Ebxj{Yyt2yI;s@:e"
                  },
                  "value": "result"
                },
                "datatype": {
                  "type": "miscellaneous",
                  "action": "parameter-type",
                  "extra": {
                    "id": "L3Z?PB8k6)D]5##_U*63"
                  },
                  "parameter-type": "string"
                },
                "required": {
                  "type": "primitive",
                  "action": "boolean",
                  "extra": {
                    "id": "=3QlBlyN%72a#E{!4ovC"
                  },
                  "value": false
                }
              }]
            }
          }]
        }],
        "statements": [{
            "type": "helper",
            "action": "code-section",
            "extra": {
              "id": ")$x]%MY;vXFM66t$[#3g"
            },
            "statements": [{
                "type": "document",
                "action": "comment-note",
                "extra": {
                  "id": "8prbh]Hq?^s2BV=`J4|$"
                },
                "comment": {
                  "type": "primitive",
                  "action": "string",
                  "value": "code 1"
                }
              },
              {
                "type": "variable",
                "action": "create",
                "extra": {
                  "id": "QubE)kG{r7H9xR-}CHF7"
                },
                "name": {
                  "type": "primitive",
                  "action": "string",
                  "extra": {
                    "id": "h=rET1COg7/YoxR{OfYz"
                  },
                  "value": "qqq2"
                }
              },
              {
                "type": "variable",
                "action": "set",
                "extra": {
                  "id": "p[gSm|F$@CU3M_F!Tl!o"
                },
                "name": {
                  "type": "primitive",
                  "action": "string",
                  "extra": {
                    "id": "Y9)#ccnXbtpN}[zq!U!a"
                  },
                  "value": "qqq2"
                },
                "value": {
                  "type": "pair",
                  "action": "create",
                  "extra": {
                    "id": "b=/HL|O--7^=~{c4]SYk"
                  },
                  "key": {
                    "type": "primitive",
                    "action": "string",
                    "extra": {
                      "id": "2,aMj@kiv$aG]2Fg(X+F"
                    },
                    "value": "qqq"
                  },
                  "value": {
                    "type": "pair",
                    "action": "create",
                    "extra": {
                      "id": "_]kqE^cuy#mK!s5~KufR"
                    },
                    "key": {
                      "type": "primitive",
                      "action": "string",
                      "extra": {
                        "id": "u9v[T7T4t]oRB)KMKAo7"
                      },
                      "value": "qqq"
                    },
                    "value": {
                      "type": "primitive",
                      "action": "string",
                      "extra": {
                        "id": "/,V=$)=-Ex*1KAQpKim7"
                      },
                      "value": "1234"
                    }
                  }
                }
              }
            ]
          },
          {
            "type": "helper",
            "action": "code-section",
            "extra": {
              "id": "Nwefwvwwe|T;2"
            },
            "statements": [{
                "type": "document",
                "action": "comment-note",
                "extra": {
                  "id": "!hba3~wcwe+!AjcF"
                },
                "comment": {
                  "type": "primitive",
                  "action": "string",
                  "value": "code 2"
                }
              },
              {
                "type": "variable",
                "action": "create",
                "extra": {
                  "id": "`.zqkDw?Qd$BD#;U`pb7"
                },
                "name": {
                  "type": "primitive",
                  "action": "string",
                  "extra": {
                    "id": "awefwvevK~0Y"
                  },
                  "value": "qqq1"
                }
              },
              {
                "type": "variable",
                "action": "set",
                "extra": {
                  "id": "S,wVwvvefdwefFx/"
                },
                "name": {
                  "type": "primitive",
                  "action": "string",
                  "extra": {
                    "id": ":QwevdsdvdWex"
                  },
                  "value": "qqq1"
                },
                "value": {
                  "type": "custom-util",
                  "action": "create",
                  "extra": {
                    "id": "xLwedwvwev|K"
                  },
                  "arguments": [{
                      "type": "pair",
                      "action": "create",
                      "extra": {
                        "id": "ig-vsdvsdO0O80D!"
                      },
                      "key": {
                        "type": "primitive",
                        "action": "string",
                        "extra": {
                          "id": "AJOawef`hZ|d,"
                        },
                        "value": "arg1"
                      },
                      "value": {
                        "type": "primitive",
                        "action": "null",
                        "extra": {
                          "id": "8MraKndfsdf;$MF4Cb"
                        }
                      }
                    },
                    {
                      "type": "pair",
                      "action": "create",
                      "extra": {
                        "id": "`eeGi4|7oGaRT7UB;AvP"
                      },
                      "key": {
                        "type": "primitive",
                        "action": "string",
                        "extra": {
                          "id": "vewdqwd4X"
                        },
                        "value": "arg2"
                      },
                      "value": {
                        "type": "primitive",
                        "action": "null",
                        "extra": {
                          "id": "Xcswefsddv,"
                        }
                      }
                    }
                  ],
                  "payload": [],
                  "return-value": {
                    "type": "primitive",
                    "action": "string",
                    "extra": {
                      "id": "eodx(7Oz7)7Gw;.dF4kt"
                    },
                    "value": "retData"
                  },
                  "property": {
                    "type": "property",
                    "action": "custom-util",
                    "extra": [],
                    "id": {
                      "type": "primitive",
                      "action": "string",
                      "extra": [],
                      "value": "3e1589wewefewfq2332cc2a83"
                    },
                    "name": {
                      "type": "primitive",
                      "action": "string",
                      "extra": [],
                      "value": "test1"
                    },
                    "description": {
                      "type": "primitive",
                      "action": "string",
                      "extra": [],
                      "value": ""
                    },
                    "created-date": {
                      "type": "primitive",
                      "action": "string",
                      "extra": [],
                      "value": ""
                    },
                    "updated-date": {
                      "type": "primitive",
                      "action": "string",
                      "extra": [],
                      "value": ""
                    }
                  }
                }
              }
            ]
          }
        ]
      }],
      "catchers": [{
        "type": "exception-handler",
        "action": "exception-catcher",
        "extra": {
          "id": "6ZCfwefwfewfO)f}UF!"
        },
        "id": "064673gerg081c4e1",
        "exceptions": [{
          "type": "miscellaneous",
          "action": "exception-type",
          "extra": {
            "id": "IQRLF7Shgd46:T!UXA~V"
          },
          "exception-type": "RuntimeException"
        }],
        "statements": []
      }]
    }];
    
    //visits the arr array recursively looking for objects having .extra.id that will be pushed in the accumulator
    function grabExtraIds(arr, accumulator = []){      
      //for each item in the array
      arr.forEach(nestedObject => {
      
        //pushes the .extra.id value of this nestedObject if existent
        const extraId = nestedObject?.extra?.id;
        if(extraId !== undefined)
          accumulator.push( extraId );    
        
        //for each property of the current nestedObject
        Object.getOwnPropertyNames(nestedObject)
          .forEach( propertyName =>{            
            const property = nestedObject[propertyName];
            //if the property value is of type array
            if (Array.isArray(property))
              //recursively call the grabExtraIds with this object
              grabExtraIds(property, accumulator);      
          });  
        });
    }
    
    const result = [];
    grabExtraIds(arr, result);
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search