-1

I am looking to access a nested JSON object I have in from a rest request to store it is as the primary key in my Dynamo DB table on AWS.

The Json Object is in the following format:

{
 "api": {
    "results": 543,
    "fixtures": [
      {
        "fixture_id": 95095,
        "venue": "Estádio Rei Pelé",
        "goalsHomeTeam": 1,
        "goalsAwayTeam": 1,
        "awayTeam": {
          "logo": "https://media.api-football.com/teams/156.png",
          "team_id": 156,
          "team_name": "Sao Bento"
        },
        "event_timestamp": 1569715200,
        "referee": null,
        "elapsed": 90,
        "score": {
          "halftime": "0-0",
          "penalty": null,
          "fulltime": "1-1",
          "extratime": null
        },
        "round": "Regular Season - 25",
        "event_date": "2019-09-29T00:00:00+00:00",
        "statusShort": "FT",
        "homeTeam": {
          "logo": "https://media.api-football.com/teams/146.png",
          "team_id": 146,
          "team_name": "CRB"
        },

And I am trying to access it using the following code but I am getting a NullPointerException on line 38 which is the line below starting int fixture_id.

My code is the following:

JsonParser parser = new JsonFactory().createParser(new File("fixturesTwo.json"));

    JsonNode rootNode = new ObjectMapper().readTree(parser);
    Iterator<JsonNode> iter = rootNode.iterator();

    ObjectNode currentNode;


    while (iter.hasNext()) {
        currentNode = (ObjectNode) iter.next();

        int fixture_id = currentNode.path("api").findPath("fixtures").findValue("fixture_id").asInt();
        int league_id = currentNode.path("api").findPath("fixtures").findValue("league_id").asInt();

        try {
            table.putItem(new Item().withPrimaryKey("fixture_id", fixture_id, "league_id", league_id)
                    .withJSON("Home Team", currentNode.path("api").findPath("fixtures").findPath("homeTeam").findValue("team_name").toString())
                    .withJSON("Away Team", currentNode.path("api").findPath("fixtures").findPath("awayTeam").findValue("team_name").toString()));
            System.out.println("PutItem succeeded: " + fixture_id + " " + league_id);

        } catch (Exception e) {
            System.err.println("Unable to add: " + fixture_id);
            System.err.println(e.getMessage());
            break;
        }
    }
    parser.close();

}

I am guessing it is something similar to what I am doing I just can't figure out what I am doing wrong.

halfer
  • 19,824
  • 17
  • 99
  • 186
Deane Kane
  • 126
  • 2
  • 15
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Sep 29 '19 at 09:59

1 Answers1

1

rootNode has node "API".currentNode:

{
   "results":543,
   "fixtures":[
      {
         "fixture_id":95095,
         "venue":"Estádio Rei Pelé",
         "goalsHomeTeam":1,
         "goalsAwayTeam":1,
         "awayTeam":{
            "logo":"https://media.api-football.com/teams/156.png",
            "team_id":156,
            "team_name":"Sao Bento"
         },
         "event_timestamp":1569715200,
         "referee":null,
         "elapsed":90,
         "score":{
            "halftime":"0-0",
            "penalty":null,
            "fulltime":"1-1",
            "extratime":null
         },
         "round":"Regular Season - 25",
         "event_date":"2019-09-29T00:00:00+00:00",
         "statusShort":"FT",
         "homeTeam":{
            "logo":"https://media.api-football.com/teams/146.png",
            "team_id":146,
            "team_name":"CRB"
         }
      }
   ]
}

The right code:

int fixture_id = currentNode.path("fixtures").findValue("fixture_id").asInt();
4b0
  • 21,981
  • 30
  • 95
  • 142
  • This only adds the first fixture_id to the table. Any idea how to iterate through and add them all? – Deane Kane Sep 29 '19 at 12:25
  • Ahh I see my iterator was pointing to the "api" node. I fixed it doing this. `Iterator iter = (rootNode.findPath("fixtures")).iterator();` – Deane Kane Sep 29 '19 at 13:15