Home > Enterprise >  Filter Nested Json with Javascript
Filter Nested Json with Javascript

Time:10-31

How can I filter this Json, only for especific keys

I want to filter, only for "in_play" = true and "coutnry" = "KR"

Below a sample of the Json (it is very large...)

Thanks a lot!

{
  "status": 200,
  "errors": [],
  "pagination": [],
  "result": [
    {
      "id": "657efe5af506ea14",
      "date": "2022-10-30 05:00:00",
      "timer": "53:45",
      "est_e_time": false,
      "in_play": true,
      "championship": {
        "id": "bbdd811b816f199",
        "name": "South Korea Cup",
        "s_name": null,
        "country": "KR"
     }
    {
      "id": "25e94c1ff62386",
      "date": "2022-10-30 05:00:00",
      "timer": "51:15",
      "est_e_time": false,
      "in_play": true,
      "championship": {
        "id": "8e912fa760018f76",
        "name": "Japan J2-League",
        "s_name": null,
        "country": "JP"
      }
          ],

CodePudding user response:

First you need to make sure you data is in valid json foramt,then we can use data.result.filter(i => i.championship.country === 'KR' && i.in_play) to do it

let data = 
{
    "status":200,
    "errors":[

    ],
    "pagination":[

    ],
    "result":[
        {
            "id":"657efe5af506ea14",
            "date":"2022-10-30 05:00:00",
            "timer":"53:45",
            "est_e_time":false,
            "in_play":true,
            "championship":{
                "id":"bbdd811b816f199",
                "name":"South Korea Cup",
                "s_name":null,
                "country":"KR"
            }
        },
        {
            "id":"25e94c1ff62386",
            "date":"2022-10-30 05:00:00",
            "timer":"51:15",
            "est_e_time":false,
            "in_play":true,
            "championship":{
                "id":"8e912fa760018f76",
                "name":"Japan J2-League",
                "s_name":null,
                "country":"JP"
            }
        }
    ]
}

let result = data.result.filter(i => i.championship.country === 'KR' && i.in_play)
console.log(result)

  • Related