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 id
s 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
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 aparent
value and testing against each object’s parent.You probably don’t need to JSON.stringify.
If your json structure is in an object
json
.You can try something like:
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 keyextra
. You can enable that by passingtrue
as the third argument tofindKeyValuesInJson
or you can put the if statement withobjectsToCheck(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:
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:
This is the found ids:
And this is the live demo: