2

I have the following Json file:

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "PARK_ID": 393, "FACILITYID": 26249,  "coordinates": [ -75.73, 45.34 ] } },
{ "type": "Feature", "properties": { "PARK_ID": 161, "FACILITYID": 3510,  "coordinates": [ -75.73, 45.37 ] } },

I'm able to read the first line, "type" : "FeatureCollection"

but I'm unsure on how to read "crs" and "features". Im trying to use the coordinates in the "features" to make a tree.

my code thusfar:

    import javax.json.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;


public class Preprocess {

    public static void main (String [] args){

        InputStream fis = null;
        try {
            fis = new FileInputStream("wadepools.json");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        JsonReader reader = Json.createReader(fis);
        JsonObject wadepool = reader.readObject();
        reader.

        System.out.println (wadepool.getString("features"));//if i put "type" here i get the output "FeatureCollection"
    }
    }

It would be best if I kept it to the native json library, as I dont have any experience with Maven or Gradle.

thanks.

Zaeem Q
  • 485
  • 5
  • 11

2 Answers2

-1
Using javax.json library
  1. Add the following libraries as dependencies or add them to the build path
    // https://mvnrepository.com/artifact/javax.json/javax.json-api
    implementation("javax.json:javax.json-api:1.1.4") // javax.json-api only contains the API (interfaces) and no implementation
    // https://mvnrepository.com/artifact/org.glassfish/javax.json
    implementation("org.glassfish:javax.json:1.1.4")// to get an implementation(but if your server comes with a pre-bundled implementation, you can skip this, but need for local development)

Ref
javax.json-apiis deprecated and it is recommended to use jakarta.json-api

// https://mvnrepository.com/artifact/jakarta.json/jakarta.json-api
compile("jakarta.json:jakarta.json-api:2.0.0")
  1. Proper JSON file, the question had incomplete ] and }.
{
  "type": "FeatureCollection",
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
  },
  "features": [
    {
      "type": "Feature",
      "properties": {
        "PARK_ID": 393,
        "FACILITYID": 26249,
        "coordinates": [
          -75.73,
          45.34
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "PARK_ID": 161,
        "FACILITYID": 3510,
        "coordinates": [
          -75.73,
          45.37
        ]
      }
    }
  ]
}
  1. source code
File fis = null;
try {
    // provide a proper path to wadepools.json file
    fis = new File("./src/wadepools.json");
    JsonObject jsonObject;
    try (JsonReader reader = Json.createReader(new FileInputStream(fis))) {
        jsonObject = reader.readObject();
    }
    // since you know that features is an array of object, we will read it as JsonArray
    final JsonArray features = jsonObject.getJsonArray("features");
    System.out.println(features);

    // From json we know that features is array of objects and it has only 1 object, to read objects from array, we first locate it using index 0, we can iterate array and read objects too, but for simplicity we are targeting only 0th index
    final JsonObject object = features.getJsonObject(0);
    System.out.println(object.get("properties"));
} catch (IOException e) {
    e.printStackTrace();
}

/*output
[{"type":"Feature","properties":{"PARK_ID":393,"FACILITYID":26249,"coordinates":[-75.73,45.34]}},{"type":"Feature","properties":{"PARK_ID":161,"FACILITYID":3510,"coordinates":[-75.73,45.37]}}]
{"PARK_ID":393,"FACILITYID":26249,"coordinates":[-75.73,45.34]}
*/
Using Jackson library

You can ignore this answer if you don't know any build tools. It would be better if you learn any build tools either Maven, Gradle, etc. https://maven.apache.org/ https://gradle.org/ The following code is for maven build. Add the following dependencies to your build file pom.xml.

pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.0</version>
</dependency>

Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Preprocess {

    private static ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            
    public static void main (String [] args){

        File fis = null;
        try {
            // provide a proper path to wadepools.json file
            fis = new File("./src/wadepools.json");
            JsonNode jsonNode = MAPPER.readTree(fis);
            System.out.println(jsonNode.get("features"));
            System.out.println(jsonNode.get("features").get(0).get("properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    }
dkb
  • 4,389
  • 4
  • 36
  • 54
-2

You can get the JSON obj from the array.

JSON:

{
    "type": "FeatureCollection",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [{
            "type": "Feature",
            "properties": {
                "PARK_ID": 393,
                "FACILITYID": 26249,
                "coordinates": [-75.73, 45.34]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "PARK_ID": 161,
                "FACILITYID": 3510,
                "coordinates": [-75.73, 45.37]
            }
        }

    ]
}

Java:

    JSONArray jarray = (JSONArray) parser.parse(new FileReader("wadepools.json"));

      for (Object o : jarray )
      {
        JSONObject value= (JSONObject) o;

        String type= (String) value.get("type");
        System.out.println(type);

       JSONArray features= (JSONArray) value.get("features");
  for (Object features: features)
        {
          System.out.println(features+"");
        }

      }

Hope this helps ..!

  • 1
    This answer doesn't give a solution using _javax.json_ library as `JSONArray` is a different object from `JsonArray` from _javax.json_ library. Plus, it is really unclear about the `parser` object used. It needs some clarification. – Kilian Aug 25 '21 at 15:56