Search code examples
angularjsjsonjson-deserialization

How to query a JSON object


In my AngularJS web app, How can I query a JSON object ?

For example in the below JSON object how can I find the value of reportTypeLabel where reportTypeId=3

JSON:

[  
   {  
      reportTypeId:5,
      reportTypeCode:"FINREP",
      reportTypeLabel:"Financial Reporting"
   },
   {  
      reportTypeId:9000002,
      reportTypeCode:"REM HE",
      reportTypeLabel:"High Earners"
   },
   {  
      reportTypeId:3,
      reportTypeCode:"COREP LE",
      reportTypeLabel:"Large Exposures - COREP"
   }
]

Solution

  • If you're going to use data manipulation extensively, I'd highly recommend using a JS library, like underscore or lodash.

    For example, using underscore:

    // assuming your object is called data
    
    var result = _.findWhere(data, {
        reportTypeId: 3
    }).reportTypeLabel;
    // result === 'Large Exposures - COREP'