0

I'm trying to loop through a nested json but i'm not able to acheive the output, anybody please help me out to bring the values from the json shown below,here i want to get bpmn:startEvent id value.

{  
   "bpmn:definitions":{  
      "@attributes":{  
         "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
         "xmlns:bpmn":"http://www.omg.org/spec/BPMN/20100524/MODEL",
         "xmlns:bpmndi":"http://www.omg.org/spec/BPMN/20100524/DI",
         "xmlns:dc":"http://www.omg.org/spec/DD/20100524/DC",
         "id":"Definitions_1",
         "targetNamespace":"http://bpmn.io/schema/bpmn"
      },
      "bpmn:process":{  
         "@attributes":{  
            "id":"Process_1",
            "isExecutable":"false"
         },
         "bpmn:startEvent":{  
            "@attributes":{  
               "id":"StartEvent_1"
            }
         }
      },
      "bpmndi:BPMNDiagram":{  
         "@attributes":{  
            "id":"BPMNDiagram_1"
         },
         "bpmndi:BPMNPlane":{  
            "@attributes":{  
               "id":"BPMNPlane_1",
               "bpmnElement":"Process_1"
            },
            "bpmndi:BPMNShape":{  
               "@attributes":{  
                  "id":"_BPMNShape_StartEvent_2",
                  "bpmnElement":"StartEvent_1"
               },
               "dc:Bounds":{  
                  "@attributes":{  
                     "x":"173",
                     "y":"102",
                     "width":"36",
                     "height":"36"
                  }
               }
            }
         }
      }
   }
}
1
  • That is not JSON Commented Sep 25, 2018 at 4:14

2 Answers 2

1
var getAllValuesOfKey = function (dataObj, queryKey) {
    var resultArr = [];
    if (!queryKey) {
        return resultArr;
    }

    function execute(dataObj, queryKey) {
        Object.keys(dataObj).forEach(function (key, index) {
            if (typeof dataObj[key] == 'object' && !(dataObj[key] instanceof Array)) {
                if (key == queryKey) {
                    resultArr.push(dataObj[key]);
                }
                execute(dataObj[key], queryKey);
            } else if (key == queryKey) {
                resultArr.push(dataObj[key]);
            }
        });
    }
    execute(dataObj, queryKey);
    return resultArr;
}

var searchKey = 'bpmn:startEvent';

console.log(getAllValuesOfKey(myJson, searchKey));

NOTE: change search key to search any key. this will return array of values.

Sign up to request clarification or add additional context in comments.

2 Comments

can you provide a code snippet for this..so that i can run, also in future if anybody see that it will be more helpfull
thanks a lot sugat its working..thanks for your help
1

var myJson = {
  "bpmn:definitions": {
    "@attributes": {
      "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
      "xmlns:bpmn": "http://www.omg.org/spec/BPMN/20100524/MODEL",
      "xmlns:bpmndi": "http://www.omg.org/spec/BPMN/20100524/DI",
      "xmlns:dc": "http://www.omg.org/spec/DD/20100524/DC",
      "id": "Definitions_1",
      "targetNamespace": "http://bpmn.io/schema/bpmn"
    },
    "bpmn:process": {
      "@attributes": {
        "id": "Process_1",
        "isExecutable": "false"
      },
      "bpmn:startEvent": {
        "@attributes": {
          "id": "StartEvent_1"
        }
      }
    },
    "bpmndi:BPMNDiagram": {
      "@attributes": {
        "id": "BPMNDiagram_1"
      },
      "bpmndi:BPMNPlane": {
        "@attributes": {
          "id": "BPMNPlane_1",
          "bpmnElement": "Process_1"
        },
        "bpmndi:BPMNShape": {
          "@attributes": {
            "id": "_BPMNShape_StartEvent_2",
            "bpmnElement": "StartEvent_1"
          },
          "dc:Bounds": {
            "@attributes": {
              "x": "173",
              "y": "102",
              "width": "36",
              "height": "36"
            }
          }
        }
      }
    }
  }
};

console.log(myJson["bpmn:definitions"]["bpmn:process"]["bpmn:startEvent"]["@attributes"].id);

7 Comments

thanks for the reply, im getting the error, ERROR TypeError: Cannot read property 'bpmn:process' of undefined
you should have the same json as you posted
yeah i've used the same json
const myObjStr = JSON.stringify(obj); im storing the json in const myObjStr
you should not get error if you are using the same JSON
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.