Using Javascript to Filter JSON with Nested Structures

Is there a way to extract specific keys from this Json data?

I am looking to filter for "in_play" = true and "country" = "KR"

Here is a snippet of the large Json dataset:

Thank you!

{
  "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"
      }
          ],

Answer №1

Ensure your data is in valid JSON format before proceeding, then implement the following code snippet:

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

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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Including HTML elements while cycling through a jQuery collection

Is there a more efficient method of integrating somewhat intricate HTML into a webpage than what I'm currently employing? function display(friends) { $(".row").empty(); $.each(friends, function(index, friend) { var html = '<d ...

Tips for designing rainbow-themed elements (hyperlinks) using CSS to incorporate multiple colors

When customizing link styles in D3.js, it's common to adjust the color of the links. For instance, in the code snippet below, I've created thick links with a width of 10 and colored them grey. link = svg.append("g") . . . .style("stroke", "grey" ...

What are some effective ways to display input text alongside input radio buttons?

Hello, I am trying to display an input with a radio button, but for some reason it is not working correctly. When I click on 'No' it shows the correct input, but when I click on 'Yes' nothing appears. I'm unsure why this is happeni ...

What are some ways to improve the efficiency of my login system?

I have attempted two different methods. (1) User inputs information -> Ajax call to verify credentials -> Server sends a header, which is captured on the client side and written using document.write(). (2) User enters information -> Ajax call to ...

Struggling to retrieve JSON data from the MercadoLibre API while consistently encountering the CORS error?

I have been attempting to access a mercadolibre API that provides JSON data I need to utilize. However, whenever I make an AJAX GET request, I keep receiving the same error: "Response to preflight request doesn't pass access control check: It does n ...

Disappear and reappear: the magic of text on page reload with javascript onclick event

I have a JavaScript function that displays text when a button is clicked. The issue I am facing is that the text appears momentarily and then disappears after the page is reloaded. Below is the paragraph that shows the text once the button is clicked: &l ...

Perform an Ajax POST request to a PHP script

Currently, I am facing a dilemma regarding how to post data from 9 checkboxes to my php script. I came across the following code: $.ajax({ type: "POST", url: "check.php", data: data, success: success, dataType: dataType }); However, I am unsure ...

Javascript error - SyntaxError: unexpected token '}' after property list is missing

In my code snippet below: var UserCharacter = { UserID: util.getCookie('u_u'); userUsingThisCharacter: function() { var data = {}; data.UserID = UserCharacter.UserID; $.ajax({ type: "GET", url: util.API_URL + "charact ...

Reset input fields while retaining placeholder text

Seeking advice on how to use this handy jQuery tool properly: $('.myDiv input').each(function () { $(this).val(""); }); Though it clears my form, I'm struggling to maintain the placeholders of the inputs. Any suggestions? C ...

My Android application encounters a crash every time I attempt to send a push

Yesterday my android app was working in the market after being tested. I haven't made any changes to the code since then, so the error must be related to the payload or device (perhaps Google services not installed or a different operating system?). W ...

What is the best way to toggle a d3 svg overlay using a leaflet layer control?

I am looking for a solution to place 3 d3 svgs on a leaflet map and control them as easily as leaflet layers. Check out this code example, which works but is not ideal. The key part is from line 75 onwards, where I create a custom layer control linked to ...

Merging two separate observableArray into a single entity in knockout js

I am currently working on merging two distinct arrays into a general array and then connecting it using foreach. My View Model: self.cancelledItem1 = ko.computed(function () { return ko.utils.arrayFilter(self.items(), function (item) { ...

SCRIPT1015: Unfinished string literal encountered while attempting to load string

Hi, I am facing an issue with the code in my ASP.NET Razor v2 cshtml file. I am trying to load a string into a paragraph from a C# list of strings. However, when the string being loaded contains certain characters, such as: + "<p>"+'Rejv&#2 ...

Error: The XML namespace prefix 'json' is not declared for the column name 'Value/@json:ValueType' in the FOR XML statement

I'm currently developing a project that involves writing a stored procedure to generate XML format output. I need to include a namespace in the 'Main' node and introduce a JSON ValueType to the sub nodes. Here's my code: --- Pretend ta ...

Placeholder fails to appear

After implementing some jQuery validation, I wanted to display a text as a placeholder when the user skipped out of an input field without entering anything. This is the code I wrote: $('input[type="text"]').on('blur', function() { ...

"Displaying slider position relative to total count before clicking on it for

Utilizing the Foundation Zurb 6/Orbit Slider. Is there a way to show the current slide count and total before the slidechange.zf.orbit event triggers? Should I integrate an event prior to this, on window load, or use another method? The first slide initi ...

Implement a feature in Vuejs where the user can easily move to the next field by simply

I need help implementing a feature that moves the focus to the next field when the enter key is pressed. I've tried the following code, but it's not working as expected. When I added a debugger in the focusNext method inputs[index + 1].focus();, ...

What steps can I take to detect errors when generating a MongoDB ObjectId in Node.js?

var selectedCriteria = Mongoose.Types.ObjectId(payloadData.skillId), If an incorrect Id is passed, the following error message will be displayed: Error: Uncaught error: Argument passed in must be a single string of 12 bytes or a string of 24 hex charac ...

The error org.json.JSONException is thrown because the value of type java.io.IOException in the form of a java.lang.String cannot be

While the json data is successfully fetched from the URL, there seems to be an issue with converting it into a "jsonarray". The syntax appears to be correct upon checking, yet an exception is persisting. Please review the code and focus on- JSONArray jA ...

Error: The global variable cannot be emptied due to an issue with the Ajax request

As someone who is relatively new to the world of Javascript/jquery and async, I have spent a significant amount of time reading through various forums. Unfortunately, I have yet to come across a solution that addresses my specific issue. The problem at ha ...