1

I'm a little new to JSON and wish to fetch the values returned to me by elasticsearch. The Structure is somewhat like this :-

{
  "took": 17,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 32,
    "max_score": 2.6414862,
    "hits": [
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "65",
        "_score": 2.6414862,
        "_source": {
          "ndex": 65,
          "name": "Alakazam",
          "date_captured": "2015-11-03",
          "has_evolution": false,
          "type": "Psychic",
          "how_to_find": [
            {
              "game": "Red",
              "method": "Evolve"
            },
            {
              "game": "Blue",
              "method": "Evolve"
            },
            {
              "game": "Gold",
              "method": "Evolve"
            },
            {
              "game": "Silver",
              "method": "Evolve"
            }
          ],
          "height": 59,
          "weight": 105.8
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "96",
        "_score": 2.3940873,
        "_source": {
          "ndex": 96,
          "name": "Drowzee",
          "date_captured": "2015-02-04",
          "has_evolution": true,
          "evolution": "Hypno",
          "type": "Psychic",
          "how_to_find": [
            {
              "game": "Red",
              "method": "Catch"
            },
            {
              "game": "Blue",
              "method": "Catch"
            },
            {
              "game": "Gold",
              "method": "Catch"
            },
            {
              "game": "Silver",
              "method": "Catch"
            }
          ],
          "height": 39,
          "weight": 71.4
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "97",
        "_score": 2.3940873,
        "_source": {
          "ndex": 97,
          "name": "Hypno",
          "date_captured": "2015-02-05",
          "has_evolution": false,
          "type": "Psychic",
          "how_to_find": [
            {
              "game": "Red",
              "method": [
                "Catch",
                "Evolve"
              ]
            },
            {
              "game": "Blue",
              "method": [
                "Catch",
                "Evolve"
              ]
            },
            {
              "game": "Gold",
              "method": [
                "Catch",
                "Evolve"
              ]
            },
            {
              "game": "Silver",
              "method": [
                "Catch",
                "Evolve"
              ]
            }
          ],
          "height": 63,
          "weight": 166.7
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "103",
        "_score": 2.3940873,
        "_source": {
          "ndex": 103,
          "name": "Exeggutor",
          "date_captured": "2015-02-11",
          "has_evolution": false,
          "type": [
            "Grass",
            "Psychic"
          ],
          "hm": {
            "id": "HM04",
            "name": "Strength"
          },
          "how_to_find": [
            {
              "game": "Red",
              "method": "Evolve"
            },
            {
              "game": "Blue",
              "method": "Evolve"
            },
            {
              "game": "Gold",
              "method": "Evolve"
            },
            {
              "game": "Silver",
              "method": "Evolve"
            }
          ],
          "height": 79,
          "weight": 264.6
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "122",
        "_score": 2.3940873,
        "_source": {
          "ndex": 122,
          "name": "Mr. Mime",
          "date_captured": "2015-04-10",
          "has_evolution": false,
          "type": [
            "Psychic",
            "Fairy"
          ],
          "how_to_find": [
            {
              "game": "Red",
              "method": "Event"
            },
            {
              "game": "Blue",
              "method": "Event"
            },
            {
              "game": "Gold",
              "method": [
                "Catch",
                "Reward"
              ]
            },
            {
              "game": "Silver",
              "method": [
                "Catch",
                "Reward"
              ]
            }
          ],
          "height": 51,
          "weight": 120.1
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "124",
        "_score": 2.3940873,
        "_source": {
          "ndex": 124,
          "name": "Jynx",
          "date_captured": "2015-04-12",
          "has_evolution": false,
          "type": [
            "Ice",
            "Psychic"
          ],
          "how_to_find": [
            {
              "game": "Red",
              "method": "Event"
            },
            {
              "game": "Blue",
              "method": "Event"
            },
            {
              "game": "Gold",
              "method": "Catch"
            },
            {
              "game": "Silver",
              "method": "Catch"
            }
          ],
          "height": 55,
          "weight": 89.5
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "16",
        "_score": 2.1465569,
        "_source": {
          "ndex": 16,
          "name": "Pidgey",
          "date_captured": "2015-06-01",
          "has_evolution": true,
          "evolution": "Pidgeotto",
          "type": [
            "Normal",
            "Flying"
          ],
          "hm": {
            "id": "HM02",
            "name": "Fly"
          },
          "how_to_find": [
            {
              "game": "Red",
              "method": "Catch"
            },
            {
              "game": "Blue",
              "method": "Catch"
            },
            {
              "game": "Gold",
              "method": "Catch"
            },
            {
              "game": "Silver",
              "method": "Catch"
            }
          ],
          "height": 12,
          "weight": 4
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "18",
        "_score": 2.1465569,
        "_source": {
          "ndex": 18,
          "name": "Pidgeot",
          "date_captured": "2015-06-03",
          "has_evolution": false,
          "type": [
            "Normal",
            "Flying"
          ],
          "hm": {
            "id": "HM02",
            "name": "Fly"
          },
          "how_to_find": [
            {
              "game": "Red",
              "method": "Evolve"
            },
            {
              "game": "Blue",
              "method": "Evolve"
            },
            {
              "game": "Gold",
              "method": "Evolve"
            },
            {
              "game": "Silver",
              "method": "Evolve"
            }
          ],
          "height": 59,
          "weight": 87.1
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "41",
        "_score": 2.1465569,
        "_source": {
          "ndex": 41,
          "name": "Zubat",
          "date_captured": "2015-09-01",
          "has_evolution": true,
          "evolution": "Golbat",
          "type": [
            "Poison",
            "Flying"
          ],
          "hm": {
            "id": "HM02",
            "name": "Fly"
          },
          "how_to_find": [
            {
              "game": "Red",
              "method": "Catch"
            },
            {
              "game": "Blue",
              "method": "Catch"
            },
            {
              "game": "Gold",
              "method": "Catch"
            },
            {
              "game": "Silver",
              "method": "Catch"
            }
          ],
          "height": 31,
          "weight": 16.5
        }
      },
      {
        "_index": "pokedex",
        "_type": "pokemon",
        "_id": "84",
        "_score": 2.1465569,
        "_source": {
          "ndex": 84,
          "name": "Doduo",
          "date_captured": "2015-01-04",
          "has_evolution": true,
          "evolution": "Dodrio",
          "type": [
            "Normal",
            "Flying"
          ],
          "hm": {
            "id": "HM02",
            "name": "Fly"
          },
          "how_to_find": [
            {
              "game": "Red",
              "method": "Catch"
            },
            {
              "game": "Blue",
              "method": "Catch"
            },
            {
              "game": "Gold",
              "method": "Catch"
            },
            {
              "game": "Silver",
              "method": "Catch"
            }
          ],
          "height": 55,
          "weight": 86.4
        }
      }
    ]
  },
  "aggregations": {
    "My_Date_Ranges": {
      "buckets": [
        {
          "key": "*-2-3-2015",
          "to": 1422972585150,
          "to_as_string": "2-3-2015",
          "doc_count": 3
        },
        {
          "key": "4-3-2015-6-3-2015",
          "from": 1428070185150,
          "from_as_string": "4-3-2015",
          "to": 1433340585150,
          "to_as_string": "6-3-2015",
          "doc_count": 8
        },
        {
          "key": "6-3-2015-*",
          "from": 1433340585150,
          "from_as_string": "6-3-2015",
          "doc_count": 17
        }
      ]
    }
  }
}

I tried using "org.json.simple.*" library but it struggles to fetch anything from such nested documents. Only outside information can be fetched.

My code :-

FileReader reader = new FileReader("varun_tmp.txt");
            JSONObject jsonObject;
            try {
                jsonObject = (JSONObject) new JSONParser().parse(reader);

            long id = (Long) jsonObject.get("took");

            System.out.println("\nTesting:-\nTook : " + id);

            JSONObject json_hits = (JSONObject) new JSONParser().

            JSONArray lang = (JSONArray) jsonObject.get("hits");

            for(int i=0; i<lang.size(); i++){
                System.out.println("The "+(i+1)+"th Element in Hits is : "+ lang.get(i)+"\n");
            }

I get this error :-

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray at groupid.artifactid.App.main(App.java:204)

1 Answer 1

2

It looks like your JSON parsing code is correct - but you're getting tripped up with the way elastic search structures their responses.

The root level "hits" is an object - and inside of that object is an array called hits. You'll need to get "hits" twice - once to get the hits object, then once to get the actual array of hits.

Give this a shot:

JSONArray lang = (JSONArray) jsonObject.get("hits").get("hits");

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

Comments

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.