Home  >  Forum  >  Angular
Post New Query

Angular - fetch the value from JSON object based on condition



Asked on May 08, 2019
Hi,
I want to fetch the value from JSON object based on condition .
For Example :
I have following JSON Object. 
{
  "text": "India",
  "value": 1,
  "locationCode": "001",
  "children": [
    {
      "text": "Karnataka",
      "value": 37,
      "locationCode": "EX01",
      "disabled": "false",
      "children": [
        {
          "text": "Hubli",
          "value": 38,
          "disabled": "false",
          "locationCode": "UBL01",
          "children": [
            {
              "text": "Vidyanagar",
              "value": 43,
              "disabled": "false",
              "locationCode": "VID01",
              "children": []
            }
          ]
        },
        {
          "text": "Bangalore",
          "value": 40,
          "disabled": "false",
          "locationCode": "BANG01",
          "children": [
            {
              "text": "JayNagar",
              "value": 48,
              "disabled": "false",
              "locationCode": "JAY000",
              "children": [
                {
                  "text": "Vidyagiri",
                  "value": 56,
                  "disabled": "false",
                  "locationCode": "VID001",
                  "children": []
                }
              ]
            }
          ]
        },
        {
          "text": "Mysore",
          "value": 57,
          "disabled": "false",
          "locationCode": "CG002",
          "children": []
        }
      ]
    }
  ]
}
From this JSON I want to pass one location name and fetch the parent (grand parents)and children
(grand children) of the passed location.
For Example:  If I pass Banglore location , then I should get following JSON response:

{
  "text": "India",
  "value": 1,
  "locationCode": "001",
  "children": [
    {
      "text": "Karnataka",
      "value": 37,
      "locationCode": "EX01",
      "disabled": "false",
      "children": [
        {
          "text": "Bangalore",
          "value": 40,
          "disabled": "false",
          "locationCode": "BANG01",
          "children": [
            {
              "text": "JayNagar",
              "value": 48,
              "disabled": "false",
              "locationCode": "JAY000",
              "children": [
                {
                  "text": "Vidyagiri",
                  "value": 56,
                  "disabled": "false",
                  "locationCode": "VID001",
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

As response It should give parents(grand parents) and children (grand children) of Bangalore location.
How to achieve this?
Can any one please provide solution for this?


Thanks & Regards,
Shilpa Kulkarni




Replied on May 09, 2019
Any specific reason to filter JSON tree in such format?
We can use JSON.parse() to get JSON Object, JSON Array etc and iterating JSON, we can get required data.



Write Answer








©2019 concretepage.com | Privacy Policy | Contact Us