help parsing a complex json structure in javascript

Hello,

  I have a complex Json structure. I need some help parsing it...

e.g
I need to return the address object when the QG.name = address etc..


[
  {
    "CanAdd": false,
    "QG": [
      {
        "Id": 3,
        "Name": "Name",
        "Q": [
          {
            "Id": 1,
            "Name": "First Name",
            "QE": [
              {
                "Id": 21,
                "Label": "First Name",
                "Value": null
              }
            ],
            "Version": "1",
            "Display": false
          },
          {
            "Id": 2,
            "Name": "Last Name",
            "QE": [
              {
                "Id": 22,
                "Label": "Last Name",
                "Value": null
              }
            ],
            "Version": "1",
            "Display": false
          }
        ],
        "Version": "1",
        "Display": false
      }
    ]
  },
  {
    "CanAdd": false,
    "QG": [
      {
        "Id": 30,
        "Name": "address",
        "Q": [
          {
            "Id": 1,
            "Name": "address",
            "QE": [
              {
                "Id": 21,
                "Label": "address1",
                "Value": null
              }
            ],
            "Version": "1",
            "Display": false
          }
        ],
        "Version": "1",
        "Display": false
      }
    ]
  },
  {
    "CanAdd": false,
    "QG": [
      {
        "Id": 30,
        "Name": "phone",
        "Q": [
          {
            "Id": 1,
            "Name": "phone1",
            "QE": [
              {
                "Id": 21,
                "Label": "phone1",
                "Value": null
              }
            ],
            "Version": "1",
            "Display": false
          }
        ],
        "Version": "1",
        "Display": false
      }
    ]
  }
]

Open in new window

chand pbAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
which address element? There seems to be an outer address and an inner address?

Here a quick sample - perhaps you can work on this:
http://jsfiddle.net/EE_RainerJ/asq8su4d/

Other question: does it have to be plain JavaScript or could you use additional libraries like jQuery?

Thanks and HTH
Rainer
chand pbAuthor Commented:
Thanks for your help

Sorry the outer address and it can be jquery

I will play with the fiddle . But I want the address object return starting from the "Canadd" parameter
My json object is bigger. So. At a later instance I may want the name object

Thanks again for your help
hieloCommented:
try:
function fetchItem(name) {
    for (var i=0; i<json.length; i++) {
        for(var j=0; j<json[i].QG.length; j++) {
            if (json[i].QG[j].Name ==name) {
                return json[i];
            }
        }
    }
}

alert( JSON.stringify( fetchItem("address") ) );

alert( JSON.stringify( fetchItem("phone") ) );

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chand pbAuthor Commented:
thanks. i have a follow-up but will ask in another question
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.