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?
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);
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);
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; }
Related
This is my first question on stack overflow. I am attempting to map an array that is housed within an object. My question is more regarding the targeting than the actual mapping process itself (I think). The goal of my code is to map an array to this target:
var target = {
"id": 1, //as an int
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": "all of the batter types as a string",
"ingredients": [],//a copy of all the toppings
"countOfFillings": 0
}
the object in question is:
var bakery = {
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0004",
"type": "bar",
"name": "Bar",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7001", "name": "None", "addcost": 0 },
{ "id": "7002", "name": "Custard", "addcost": 0.25 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0.25 }
]
}
},
{
"id": "0005",
"type": "twist",
"name": "Twist",
"ppu": 0.65,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
]
},
{
"id": "0006",
"type": "filled",
"name": "Filled",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7002", "name": "Custard", "addcost": 0 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0 },
{ "id": "7004", "name": "Strawberry Jelly", "addcost": 0 },
{ "id": "7005", "name": "Rasberry Jelly", "addcost": 0 }
]
}
}
]
}
}
And this is my attempt at mapping. I'm able to get the array generated but only from the first item in the array as indicated with the [0]. Any feedback or advice would be greatly appreciated!
var exampleTest = bakery.items.item.map(mapCake);
function mapCake (aCake)
{
let newType = `${bakery.items.item[1].type}`
let result = {id: `${bakery.items.item[0].id}`,
type: newType,
name: `${bakery.items.item[0].name}`,
ppu: `${bakery.items.item[0].ppu}`,
batters: [`${bakery.items.item[0].batters.batter}`],
ingredients: `${bakery.items.item[0].topping}`,
countOfFillings: `${bakery.items.item[0].fillings}`.length};
return result
}
Try this:
function mapCake(aCake) {
let newType = `${aCake.type}`;
let result = {
id: `${aCake.id}`,
type: newType,
name: `${aCake.name}`,
ppu: `${aCake.ppu}`,
batters: [`${aCake.batters.batter}`],
ingredients: `${aCake.topping}`,
countOfFillings: `${aCake.fillings}`.length
};
return result;
}
Looks like in your function you're directly indexing a specific bakery item each time.
Your aCake parameter wasn't yet being used, at each iteration, aCake refers to the current item that is being iterated over - so that's what you want to be populating your result values with.
how can I loop through this JSON in JavaScript and check whether a user's id exists? This data is from an image annotation collection and I am going to need to prevent a user from editing someone else's annotations.
In this example, lets just say that user id I am looking for is K2Lb1R7owqR9BYmpJAJzrg6w1s92 from the creator: key.
I tried using for example arr.includes() but didn't work for me.
What is my expected output? I think for now I need to just worry whether a user's id exists or not and if so, return false or true.
The creator object will always be present inside the body.
Thank you for your help.
[{
"body": [{
"created": "2020-07-03T11:49:32.058Z",
"purpose": "commenting",
"value": "don't move me",
"creator": {
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
"name": "testme"
},
"type": "TextualBody"
}, {
"purpose": "tagging",
"value": "move",
"creator": {
"name": "testme",
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
},
"type": "TextualBody",
"created": "2020-07-03T11:49:30.850Z"
}],
"id": "IRCJPsz4NKkiuhVLE1hR",
"photoDocId": "92wNwz2aaqy7CWf3mGo1",
"#context": "http://www.w3.org/ns/anno.jsonld",
"type": "Annotation",
"target": {
"source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0",
"selector": {
"conformsTo": "http://www.w3.org/TR/media-frags/",
"type": "FragmentSelector",
"value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344"
}
}
}, {
"body": [{
"purpose": "commenting",
"type": "TextualBody",
"creator": {
"name": "doss",
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
},
"value": "anyone ever climb this mountain top?",
"created": "2020-07-03T10:57:40.590Z"
}, {
"creator": {
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92",
"name": "doss"
},
"purpose": "tagging",
"value": "ridge",
"type": "TextualBody",
"created": "2020-07-03T10:57:39.351Z"
}, {
"value": "i did in 2005",
"created": "2020-07-03T10:59:45.318Z",
"type": "TextualBody",
"creator": {
"name": "testme",
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
},
"purpose": "commenting"
}, {
"type": "TextualBody",
"created": "2020-07-03T10:59:43.966Z",
"purpose": "tagging",
"value": "testme",
"creator": {
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
"name": "testme"
}
}, {
"type": "TextualBody",
"purpose": "replying",
"value": "test",
"creator": {
"name": "doss",
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
},
"created": "2020-07-03T11:39:18.860Z"
}],
"type": "Annotation",
"#context": "http://www.w3.org/ns/anno.jsonld",
"photoDocId": "92wNwz2aaqy7CWf3mGo1",
"target": {
"selector": {
"value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156",
"conformsTo": "http://www.w3.org/TR/media-frags/",
"type": "FragmentSelector"
},
"source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"
},
"id": "IlI7SRjFm8qohmHDLBw2"
}]
You can make use of flatMap to get flat data in array then using some to find out whether the value is present or not:
var array=[{ "body": [{ "created": "2020-07-03T11:49:32.058Z", "purpose": "commenting", "value": "don't move me", "creator": { "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2", "name": "testme" }, "type": "TextualBody" }, { "purpose": "tagging", "value": "move", "creator": { "name": "testme", "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2" }, "type": "TextualBody", "created": "2020-07-03T11:49:30.850Z" }], "id": "IRCJPsz4NKkiuhVLE1hR", "photoDocId": "92wNwz2aaqy7CWf3mGo1", "#context": "http://www.w3.org/ns/anno.jsonld", "type": "Annotation", "target": { "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0", "selector": { "conformsTo": "http://www.w3.org/TR/media-frags/", "type": "FragmentSelector", "value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344" } }}, { "body": [{ "purpose": "commenting", "type": "TextualBody", "creator": { "name": "doss", "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92" }, "value": "anyone ever climb this mountain top?", "created": "2020-07-03T10:57:40.590Z" }, { "creator": { "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92", "name": "doss" }, "purpose": "tagging", "value": "ridge", "type": "TextualBody", "created": "2020-07-03T10:57:39.351Z" }, { "value": "i did in 2005", "created": "2020-07-03T10:59:45.318Z", "type": "TextualBody", "creator": { "name": "testme", "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2" }, "purpose": "commenting" }, { "type": "TextualBody", "created": "2020-07-03T10:59:43.966Z", "purpose": "tagging", "value": "testme", "creator": { "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2", "name": "testme" } }, { "type": "TextualBody", "purpose": "replying", "value": "test", "creator": { "name": "doss", "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92" }, "created": "2020-07-03T11:39:18.860Z" }], "type": "Annotation", "#context": "http://www.w3.org/ns/anno.jsonld", "photoDocId": "92wNwz2aaqy7CWf3mGo1", "target": { "selector": { "value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156", "conformsTo": "http://www.w3.org/TR/media-frags/", "type": "FragmentSelector" }, "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0" }, "id": "IlI7SRjFm8qohmHDLBw2"}];
var result = array.flatMap(elem=>elem.body).some(user=>user.creator.id==='K2Lb1R7owqR9BYmpJAJzrg6w1s92');
console.log(result);
some will return either true or false, which is what you need here.
I hope this helps.
You can make use of the some function:
const data = [{"body":[{"created":"2020-07-03T11:49:32.058Z","purpose":"commenting","value":"don't move me","creator":{"id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2","name":"testme"},"type":"TextualBody"},{"purpose":"tagging","value":"move","creator":{"name":"testme","id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2"},"type":"TextualBody","created":"2020-07-03T11:49:30.850Z"}],"id":"IRCJPsz4NKkiuhVLE1hR","photoDocId":"92wNwz2aaqy7CWf3mGo1","#context":"http://www.w3.org/ns/anno.jsonld","type":"Annotation","target":{"source":"https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0","selector":{"conformsTo":"http://www.w3.org/TR/media-frags/","type":"FragmentSelector","value":"xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344"}}},{"body":[{"purpose":"commenting","type":"TextualBody","creator":{"name":"doss","id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92"},"value":"anyone ever climb this mountain top?","created":"2020-07-03T10:57:40.590Z"},{"creator":{"id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92","name":"doss"},"purpose":"tagging","value":"ridge","type":"TextualBody","created":"2020-07-03T10:57:39.351Z"},{"value":"i did in 2005","created":"2020-07-03T10:59:45.318Z","type":"TextualBody","creator":{"name":"testme","id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2"},"purpose":"commenting"},{"type":"TextualBody","created":"2020-07-03T10:59:43.966Z","purpose":"tagging","value":"testme","creator":{"id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2","name":"testme"}},{"type":"TextualBody","purpose":"replying","value":"test","creator":{"name":"doss","id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92"},"created":"2020-07-03T11:39:18.860Z"}],"type":"Annotation","#context":"http://www.w3.org/ns/anno.jsonld","photoDocId":"92wNwz2aaqy7CWf3mGo1","target":{"selector":{"value":"xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156","conformsTo":"http://www.w3.org/TR/media-frags/","type":"FragmentSelector"},"source":"https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"},"id":"IlI7SRjFm8qohmHDLBw2"}]
console.log(data.some(({body}) => body.some(({creator}) => creator.id === 'K2Lb1R7owqR9BYmpJAJzrg6w1s92')));
I am trying to iterate the json below, I have got this as an output of an astify of sql parser. Now i want to validate the column type with a DB schema that i have.
{
"with": null,
"type": "select",
"options": null,
"distinct": null,
"columns": "*",
"from": [
{
"db": null,
"table": "TABLE_1",
"as": null
}
],
"where": {
"type": "binary_expr",
"operator": "AND",
"left": {
"type": "binary_expr",
"operator": "AND",
"left": {
"type": "binary_expr",
"operator": "=",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_1"
},
"right": {
"type": "string",
"value": "NORM"
}
},
"right": {
"type": "binary_expr",
"operator": "IN",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_2"
},
"right": {
"type": "expr_list",
"value": [
{
"type": "string",
"value": "11"
},
{
"type": "string",
"value": "12"
},
{
"type": "string",
"value": "13"
},
{
"type": "string",
"value": "14"
},
{
"type": "string",
"value": "15"
},
{
"type": "string",
"value": "16"
},
{
"type": "string",
"value": "17"
},
{
"type": "string",
"value": "18"
},
{
"type": "string",
"value": "42"
},
{
"type": "string",
"value": "43"
},
{
"type": "string",
"value": "44"
},
{
"type": "string",
"value": "45"
},
{
"type": "string",
"value": "101"
},
{
"type": "string",
"value": "102"
},
{
"type": "string",
"value": "103"
},
{
"type": "string",
"value": "104"
},
{
"type": "string",
"value": "128"
},
{
"type": "string",
"value": "129"
},
{
"type": "string",
"value": "130"
},
{
"type": "string",
"value": "131"
},
{
"type": "string",
"value": "159"
}
]
}
}
},
"right": {
"type": "binary_expr",
"operator": "OR",
"left": {
"type": "binary_expr",
"operator": "IS NOT",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_3"
},
"right": {
"type": "null",
"value": null
}
},
"right": {
"type": "binary_expr",
"operator": "=",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_4"
},
"right": {
"type": "number",
"value": 1
}
},
"parentheses": true
}
},
"groupby": null,
"having": null,
"orderby": null,
"limit": null
}
Can you please help me get an output like which will help me identify the column and its type whether its string or not. Basically i need to validate the query whether its mapped against the correct data type while using a var condition.
Output like:
map = <key,value>
<Column_1, String>
<Column_2, String>
<Column_3, INT>
etc.
#Suzy, please check below code for your reference:
var arr = {
"with": null,
"type": "select",
"options": null,
"distinct": null,
"columns": "*",
"from": [
{
"db": null,
"table": "TABLE_1",
"as": null
}
],
"where": {
"type": "binary_expr",
"operator": "AND",
"left": {
"type": "binary_expr",
"operator": "AND",
"left": {
"type": "binary_expr",
"operator": "=",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_1"
},
"right": {
"type": "string",
"value": "NORM"
}
},
"right": {
"type": "binary_expr",
"operator": "IN",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_2"
},
"right": {
"type": "expr_list",
"value": [
{
"type": "string",
"value": "11"
},
{
"type": "string",
"value": "12"
},
{
"type": "string",
"value": "13"
},
{
"type": "string",
"value": "14"
},
{
"type": "string",
"value": "15"
},
{
"type": "string",
"value": "16"
},
{
"type": "string",
"value": "17"
},
{
"type": "string",
"value": "18"
},
{
"type": "string",
"value": "42"
},
{
"type": "string",
"value": "43"
},
{
"type": "string",
"value": "44"
},
{
"type": "string",
"value": "45"
},
{
"type": "string",
"value": "101"
},
{
"type": "string",
"value": "102"
},
{
"type": "string",
"value": "103"
},
{
"type": "string",
"value": "104"
},
{
"type": "string",
"value": "128"
},
{
"type": "string",
"value": "129"
},
{
"type": "string",
"value": "130"
},
{
"type": "string",
"value": "131"
},
{
"type": "string",
"value": "159"
}
]
}
}
},
"right": {
"type": "binary_expr",
"operator": "OR",
"left": {
"type": "binary_expr",
"operator": "IS NOT",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_3"
},
"right": {
"type": "null",
"value": null
}
},
"right": {
"type": "binary_expr",
"operator": "=",
"left": {
"type": "column_ref",
"table": null,
"column": "COLUMN_4"
},
"right": {
"type": "number",
"value": 1
}
},
"parentheses": true
}
},
"groupby": null,
"having": null,
"orderby": null,
"limit": null
};
function findProp(obj, key, out) {
var i,
proto = Object.prototype,
ts = proto.toString,
hasOwn = proto.hasOwnProperty.bind(obj);
if ('[object Array]' !== ts.call(out)) out = [];
for (i in obj) {
if (hasOwn(i)) {
if (i === key) {
out.push(obj[i]);
} else if ('[object Array]' === ts.call(obj[i]) || '[object Object]' === ts.call(obj[i])) {
findProp(obj[i], key, out);
}
}
}
return out;
}
console.log(findProp(arr, "column"));
Parse the JSON into an object using JSON.parse(), then iterate through them while checking if the value is JSON or not.
Check if the value is JSON by using catch on JSON.parse()
To get the object keys for iteration, Use var keys = Object.keys();
In this task they want me to load the video that is in this JSON object into the corresponding player after the thumbnail is clicked. I don't have a real clue of how to do this if it's possible and I've researched it for the past hour and I cannot for the life of me find anything. My code is below
var json = {
"available": true,
"screens": [{
"id": "s01",
"type": "Selector",
"config": {
"views": [{
"id": "sintel",
"type": "selectorItem",
"thumbnail": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/img/thumb_sintel.png",
"title": "Sintel",
"description": "Small video. HTML5, native controls, start poster. Bottom, right social. Video + grid",
"info": "00:51"
}, {
"id": "bbb",
"type": "selectorItem",
"thumbnail": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/img/thumb_bbb.png",
"title": "Big Buck Bunny",
"description": "Large video. HTML5, custom controls, social hover. Video + replay",
"info": "00:33"
}, {
"id": "walle",
"type": "selectorItem",
"thumbnail": "http://test-cdn.selectablemedia.com/test/a/walle/assets/img/thumb_walle.png",
"title": "WALL-E",
"description": "Small video. Youtube, custom controls. Left social. Video + carousel.",
"info": "02:30"
}]
}
},
{
"id": "sintel",
"type": "BrandWrapper",
"config": {
"views": [{
"id": "sintelBranding",
"type": "fullBranding",
"url": "sintel_single.png"
}, {
"id": "sintelPlayer",
"type": "smPlayerIframe",
"files": [{
"type": "mp4",
"codecs": "h264,aac",
"url": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/video/sintel_trailer-1080p.mp4"
},
{
"type": "mp4",
"codecs": "avc1.42E01E,mp4a.40.2",
"url": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/video/sintel_trailer-1080p.mp4"
},
{
"type": "ogv",
"codecs": "theora,vorbis",
"url": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/video/sintel_trailer-1080p.ogv"
},
{
"type": "flv",
"codecs": "h264,aac",
"url": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/video/sintel_trailer-1080p.flv"
}
]
}, {
"id": "sintelLeftClickArea",
"type": "brandClickArea",
"area": "leftTower",
"clickParams": {
"value": "https://durian.blender.org/"
}
}, {
"id": "sintelHeaderClickArea",
"type": "brandClickArea",
"area": "header",
"clickParams": {
"value": "https://durian.blender.org/about/"
}
}, {
"id": "sintelFooterClickArea",
"type": "brandClickArea",
"area": "footer",
"clickParams": {
"value": "https://durian.blender.org/gallery/"
}
}]
}
},
{
"id": "bbb",
"type": "BrandWrapper",
"config": {
"views": [{
"id": "bbbBrandingHeader",
"type": "header",
"url": "bbb_header.png"
}, {
"id": "bbbBrandingLeft",
"type": "leftTower",
"url": "bbb_tower_left.png"
}, {
"id": "bbbBrandingRight",
"type": "rightTower",
"url": "bbb_tower_right.png"
}, {
"id": "bbbBrandingFooter",
"type": "footer",
"url": "bbb_footer.png"
}, {
"id": "bbbPlayer",
"type": "smPlayerIframe",
"files": [{
"type": "mp4",
"codecs": "h264,aac",
"url": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/video/bbb_trailer_1080p.mp4"
},
{
"type": "mp4",
"codecs": "avc1.42E01E,mp4a.40.2",
"url": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/video/bbb_trailer_1080p.mp4"
},
{
"type": "ogv",
"codecs": "theora,vorbis",
"url": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/video/bbb_trailer_1080p.ogv"
},
{
"type": "flv",
"codecs": "h264,aac",
"url": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/video/bbb_trailer_1080p.flv"
}
]
}, {
"id": "bbbLeftClickArea",
"type": "brandClickArea",
"area": "leftTower",
"clickParams": {
"value": "https://peach.blender.org/"
}
}, {
"id": "bbbHeaderClickArea",
"type": "brandClickArea",
"area": "header",
"clickParams": {
"value": "https://peach.blender.org/"
}
}, {
"id": "bbbRightClickArea",
"type": "brandClickArea",
"area": "rightTower",
"clickParams": {
"value": "https://peach.blender.org/"
}
}, {
"id": "bbbFooterClickArea",
"type": "brandClickArea",
"area": "footer",
"clickParams": {
"value": "https://peach.blender.org/"
}
}]
}
},
{
"id": "walle",
"type": "BrandWrapper",
"config": {
"assetPath": "http://test-cdn.selectablemedia.com/test/a/walle/assets/img/",
"sizeTemplate": "smallVideo",
"views": [{
"id": "walleBranding",
"type": "fullBranding",
"url": "walle_single.png"
}, {
"id": "wallePlayer",
"type": "smPlayerIframe",
"files": [{
"type": "mp4",
"codecs": "h264,aac",
"url": "http://test-cdn.selectablemedia.com/test/a/walle/assets/video/wall-e-trailer-3_9_1280x544.mp4"
},
{
"type": "mp4",
"codecs": "avc1.42E01E,mp4a.40.2",
"url": "http://test-cdn.selectablemedia.com/test/a/walle/assets/video/wall-e-trailer-3_9_1280x544.mp4"
},
{
"type": "ogv",
"codecs": "theora,vorbis",
"url": "http://test-cdn.selectablemedia.com/test/a/walle/assets/video/wall-e-trailer-3_9_1280x544.ogv"
},
{
"type": "flv",
"codecs": "h264,aac",
"url": "http://test-cdn.selectablemedia.com/test/a/walle/assets/video/wall-e-trailer-3_9_1280x544.flv"
}
]
}, {
"id": "wallLeftClickArea",
"type": "brandClickArea",
"area": "leftTower",
"clickParams": {
"value": "http://movies.disney.com/wall-e/"
}
}, {
"id": "walleHeaderClickArea",
"type": "brandClickArea",
"area": "header",
"clickParams": {
"value": "http://movies.disney.com/wall-e/"
}
}, {
"id": "walleRightClickArea",
"type": "brandClickArea",
"area": "rightTower",
"clickParams": {
"value": "http://movies.disney.com/wall-e/"
}
}, {
"id": "walleFooterClickArea",
"type": "brandClickArea",
"area": "footer",
"clickParams": {
"value": "http://movies.disney.com/wall-e/"
}
}]
}
}
]
}
HTML
<div data-bind="foreach: screens">
<div class="container" data-bind="with: config">
<div class="row" data-bind="foreach: views">
<div class="col-md-6">
<div class="card">
<!--This line should load the video-->
<img class="card-img-top" data-bind="attr: { src: thumbnail }">
<div class="card-body">
<h5 class="card-title" data-bind="text:title"> </h5>
<p class="card-text" data-bind="text:info"> </p>
</div><!-- card content -->
</div>
</div>
</div>
</div>
</div>
Anything at all would be very appreciated.
Based on demo here, you could using the <video> tag:
<!-- ko if: type() === 'smPlayerIframe' -->
<video width="320" height="240" controls>
<!-- ko foreach: files -->
<source data-bind="attr: { src: url, type: 'video/' + type() }" />
<!-- /ko -->
Your browser does not support the video tag.
</video>
<!-- /ko -->
I have JSON Data like below
{
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}]
}
}
I want to select item object having id 0001.So how can I select or filter only this single item object using javascript or Jquery??
please help me and thanks in advance
Like this:
var item = data.items.item.filter(function(x){
return x.id === '0001';
})[0];