0

My Json FILE (it´s an array! )

[

{
    "datasetid": "country-flags",
    "recordid": "d661d0a8676bf4d7563114c1d9c465987df22132",
    "fields": {
        "num_un": 32,
        "geolocation": [
            -38.416097,
            -63.616672
        ],
        "dialing_code": "54",
        "a3_un": "ARG",
        "country": "Argentina",
        "flag": {
            "mimetype": "image/png",
            "format": "PNG",
            "filename": "ar.png",
            "width": 16,
            "id": "fceb4235ce95c8597bfa77d0db0181a0",
            "height": 11,
            "thumbnail": true
        },
        "a2_iso": "AR"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [
            -63.616672,
            -38.416097
        ]
    },
    "record_timestamp": "2016-09-26T07:48:38.162+02:00"
},
more...

]

So i want to get the value from coordinates. So for this i tried to work with this:

    JsonReader jsonReader = Json
                .createReader(new FileReader(getClass().getResource("country-flags.json").getPath()));
        JsonArray arr = jsonReader.readArray();

        for(int i = 1; i<arr.size();i++)
        {
             JsonObject obj = arr.getJsonObject(i);
             System.out.println("coordinates: " + obj.containsKey("\"coordinates\""));

             System.out.println("##########");
             System.out.println(obj.getValue("\"coordinates\""));


        }

But i got the error:

javax.json.JsonException: A non-empty JSON Pointer must begin with a '/'

Can someone help me out ?!

1
  • What do you expect obj.getValue("\"coordinates\"") to do? Commented Apr 8, 2020 at 19:08

1 Answer 1

2

Your code obj.containsKey("\"coordinates\"") will return false as coordinates is NOT a top level key, but is a 2nd level (nested) key. If you print obj.keySet(), you will get [datasetid, recordid, fields, geometry, record_timestamp] (first / top level keys only).

If the structure of your JSON is fixed, you can use the following code:

for(int i = 1; i<arr.size();i++)
{
     JsonObject obj = arr.getJsonObject(i);
     JsonObject jsonChildObject = obj.getJsonObject("geometry");
     if(jsonChildObject.containsKey("coordinates"))
          System.out.println(jsonChildObject.getValue("/coordinates"));
}

Notice the / in front of the getValue method's coordinates param. I think that was the reason you were here in the first place.

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

2 Comments

Can you explain why i there is the leading "/" ?!
The leading "/" is the syntax as specified in the javadocs at javaee.github.io/javaee-spec/javadocs/javax/json/…. You can also check these two articles which explain the JSONPointer better. 1) baeldung.com/json-pointer 2) readlearncode.com/java-ee/… Hope that helps !

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.