parse complete json and search keys matching a value

Viewed 2300

I want to traverse whole JSON in javascript and find the values starting a particular key. Suppose this is my JSON data:

var data = {
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "null": null,
  "number": 123,
  "object": {
    "a": "b",
    "abc":{
      "mohit_ab": {
        "id1":"i am id 1",
        "abcd":{
          "a" :"b",
          "c":"d"
        },
        "who" : "who are you",
        "id2":"i am id 2"
      },
      "mohit_231abcd": {
        "id1":"i am id 3",
        "identity" : "i am real mohit",
        "id2":"i am id 4"
      }
    },
    "hji":{
      "mohit_adss231": {
        "id1":"i am id 5",
        "identity" : "i am mohit in real",
        "id2":"i am id 6"
      },
      "mohit": {
        "id7":"i am id 7",
        "identity" : "i am mohit and you?",
        "id8":"i am id 8"
      }
    },
    "c": "d",
    "e": "f"
  },
  "string": "Hello World"
};

I want to parse complete JSON in javascript and get the values whose key starts with "mohit". e.g. output of above input is :

valueableData:{
  "mohit_ab": {
        "id1":"i am id 1",
        "abcd":{
          "a" :"b",
          "c":"d"
        },
        "who" : "who are you",
        "id2":"i am id 2"
      },
      "mohit_231abcd": {
        "id1":"i am id 3",
        "identity" : "i am real mohit",
        "id2":"i am id 4"
      },
      "mohit_adss231": {
        "id1":"i am id 5",
        "identity" : "i am mohit in real",
        "id2":"i am id 6"
      },
      "mohit": {
        "id7":"i am id 7",
        "identity" : "i am mohit and you?",
        "id8":"i am id 8"
      }
}

Please suggest me the easy way to achieve this.

3 Answers
Related