1

I'm creating a webpage which displays data which I have parsed from a JSON. I am using 2 JSON files. One I can parse with no problems however, the second I am struggling with.
I want to be able to parse the JSON looking for a specific object string and return all the other object strings within the same dictionary.

The layout of the JSON is:

{
  "example":[
    {
      "Area":"Inside",
      "Player":"1",
      "Status":1,
      "Start_Time":"2016-12-21",
      "End_Time":"2016-12-22",
    },
    {
      "Area":"Outside",
      "Player":"1",
      "Status":1,
      "Start_Time":"2016-12-24",
      "End_Time":"2016-12-25",
    },
    {
      "Area":"Outside",
      "Player":"2",
      "Status":1,
      "Start_Time":"2016-12-26",
      "End_Time":"2016-12-28",
    }
  ]
}

I want to say

if (player=="1") {//show ALL areas and start and end time}
//output should be something like: Area: Inside, Player: 1, Status: 1, Start_Time: 'Time', End_Time: 'Time', Area: Outside, Player: 1, Status: 1, Start_Time: 'Time', End_Time: 'Time'

I am trying to parse the JSON in javascript, this is how I am parsing the other JSON:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    var dateTime = myObj.Date;
  }
};
xmlhttp.open("GET", "http://web/server/file.json", true);
xmlhttp.send();

Any help is appreciated.

1
  • Check the edits Commented Feb 19, 2019 at 10:01

3 Answers 3

3

You may want to try filter

myObj.example.filter(obj => obj.Player == "1")

// 0: {Area: "Inside", Player: "1", Status: 1, Start_Time: "2016-12-21", End_Time: "2016-12-22"}
// 1: {Area: "Outside", Player: "1", Status: 1, Start_Time: "2016-12-24", End_Time: "2016-12-25"}


Sign up to request clarification or add additional context in comments.

Comments

0

Using forEach loop

var a={
  "example":[
    {
      "Area":"Inside",
      "Player":"1",
      "Status":1,
      "Start_Time":"2016-12-21",
      "End_Time":"2016-12-22",
    },
    {
      "Area":"Outside",
      "Player":"1",
      "Status":1,
      "Start_Time":"2016-12-24",
      "End_Time":"2016-12-25",
    },
    {
      "Area":"Outside",
      "Player":"2",
      "Status":1,
      "Start_Time":"2016-12-26",
      "End_Time":"2016-12-28",
    }
  ]
};
a.example.forEach(e=>e.Player=="1" && e.Area=="Inside"?console.log(e):false)

4 Comments

As my json is a seperate file to my javascript would I use var test = myObj.example.forEach(e=>e.Player=="1"?console.log(e):false)
You have to export the JSON from the file and in your js file you have to import it like the following import json from './jsonfile.js'. Then you can use the above code
Brilliant! Thanks, this works! How would it work if I want when Player=="1" and Area=="Inside". I tried using && as this made most sense to me but I got an error back.
forEach is not the canonical solution for this problem. The filter method was created to solve exactly this problem. forEach is a fine (albeit suboptimal) solution to the task of just logging elements that fulfill the condition. However, it doesn't let us do anything with those elements. Additionally, the OP's wording implied that they also wanted help retrieving and readying the JSON for manipulation.
-1

You really have two problems here: fetching the JSON and then parsing it to find the data you're interested in. The following snippet takes care of the first task with fetch() (the modern successor to XMLHttpRequest) and the latter task with Array.prototype.filter.

Note that if you're not using a modern javascript environment, this solution will fail. Also note that this could could be improved using the newish async/await. I excluded them for simplicity's sake.

fetch("http://web/server/file.json") // supported in modern browsers.
  .then(res => res.json()) // parses into JSON. Fails if given improper json
  .then(data => {
    console.log(data.example.filter(el => el.Player === 1)); // filter returns an array with only elements that meet the condition
  });

6 Comments

I get this error TypeError: data.filter is not a function at fetch.then.then.data
Should be fixed - didn't see that your array was nested.
Had to change Player===1 to Player==="1" and it works! How would I do where I want when Player="1" and Area="Outside"?
You would put all your conditions after the arrow (=>). For example: data.example.filter(el => el.Player === "1" && el.Area==="Outside");
Do you know how to control the error that appears when the json is invalid? see: stackoverflow.com/questions/54904894/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.