Jacob Macallan
Jacob Macallan

Reputation: 979

Grabbing Information From Json Dump With Multi-Conditions

So I'm dabbling with Javascript and JSON and trying to retrieve information from a JSON dump. I have very limited experience with JavaScript, but I was curious how one would filter results with these given queries (query and response taken off of IBM Watson's Discovery Demo).

Query

{
  "count": 5,
  "return": "title,enrichedTitle.text,url,host,blekko.chrondate",
  "query": "\"uci\",language:english",
  "aggregations": [
    "nested(enrichedTitle.entities).filter(enrichedTitle.entities.type:Company).term(enrichedTitle.entities.text)",
    "nested(enrichedTitle.entities).filter(enrichedTitle.entities.type:Person).term(enrichedTitle.entities.text)",
    "term(enrichedTitle.concepts.text)",
    "term(blekko.basedomain).term(docSentiment.type)",
    "term(docSentiment.type)",
    "min(docSentiment.score)",
    "max(docSentiment.score)",
    "filter(enrichedTitle.entities.type::Company).term(enrichedTitle.entities.text).timeslice(blekko.chrondate,1day).term(docSentiment.type)"
  ],
  "filter": "blekko.hostrank>20,blekko.chrondate>1490425200,blekko.chrondate<1495695600"
}

Response

{
  "companies": [
    {
      "key": "Reuters",
      "matching_results": 23
    },
    {
      "key": "Amgen Tour",
      "matching_results": 14
    },
    {
      "key": "Qatar Airways",
      "matching_results": 13
    },
    {
      "key": "AMC",
      "matching_results": 10
    },
    {
      "key": "British Cycling",
      "matching_results": 10
    },
    {
      "key": "HSBC UK",
      "matching_results": 9
    },
    {
      "key": "Track Cycling Worlds",
      "matching_results": 9
    },
    {
      "key": "Univision",
      "matching_results": 8
    },
    {
      "key": "Giro",
      "matching_results": 6
    },
    {
      "key": "BMC",
      "matching_results": 5
    }
  ],
  "people": [
    {
      "key": "George Bennett",
      "matching_results": 15
    },
    {
      "key": "Vogel",
      "matching_results": 12
    },
    {
      "key": "Chris Taylor",
      "matching_results": 11
    },
    {
      "key": "Brent Bookwalter",
      "matching_results": 10
    },
    {
      "key": "Rachel Atherton",
      "matching_results": 10
    },
    {
      "key": "Barker",
      "matching_results": 9
    },
    {
      "key": "Russell Westbrook",
      "matching_results": 9
    },
    {
      "key": "John Coates",
      "matching_results": 8
    },
    {
      "key": "Tracey Gaudry",
      "matching_results": 8
    },
    {
      "key": "Laura Kenny",
      "matching_results": 7
    }
  ],
  "topics": [
    {
      "key": "University of California, Irvine",
      "matching_results": 44
    },
    {
      "key": "Amgen",
      "matching_results": 43
    },
    {
      "key": "Tour of California",
      "matching_results": 43
    },
    {
      "key": "Track cycling",
      "matching_results": 42
    },
    {
      "key": "Bicycle",
      "matching_results": 39
    },
    {
      "key": "Giro d'Italia",
      "matching_results": 37
    },
    {
      "key": "World cup competition",
      "matching_results": 37
    },
    {
      "key": "Control premium",
      "matching_results": 36
    },
    {
      "key": "Irvine, California",
      "matching_results": 36
    },
    {
      "key": "Mergers and acquisitions",
      "matching_results": 33
    }
  ]
}

I'm guessing that the response portion comes from the JSON dump and the query is retrieving information from it. I'm trying to write some code that will filter out stuff similar to the query I listed above. In what format would I have to use to extract information from the JSON dump?

I've tried looking around for answers but it seems that this query is only partial of the code around it (more specifically just the code that does the calculations) rather than the actual set up.

Is there a method I should be using?

I have the JSON object stored to a variable atm that contains all the information needed.

Upvotes: 0

Views: 55

Answers (1)

Sayuri Mizuguchi
Sayuri Mizuguchi

Reputation: 5330

Say the given JSON data is in var atm = {your json}; and you want to access the companies and topics array from data.

Array companies is accessed using

alert(atm.companies);

And array topics is accessed using

alert(atm.topics);

And array for people is accessed using:

alert(atm.people);

Your companies and topics is one array. To get value from array you need to use [] and use one for to get all values inside companies, for example.

Based on you have the response (Data from JSON) within the variable atm, do this for get all values from your JSON:

Companies:

for (var i = 0; i < atm.companies.length; i++) { 
    console.log("Keys: " + atm.companies[i].key);
    console.log("Matching Results: " + atm.companies[i].matching_results);
}

Topics:

for (var i = 0; i < atm.topics.length; i++) { 
    console.log("Keys: " + atm.topics[i].key);
    console.log("Matching Results: " + atm.topics[i].matching_results);
}

And every result with the same JSON structure you will do the same for get all values.

Upvotes: 0

Related Questions