I am trying to read the data from a shapefile and once read, I want to convert it to a JSON file.

To read the data I'm using the geoTools library, and to write to JSON, I use the geojson-jackson library (https://github.com/opendatalab-de/geojson-jackson).

I have managed to correctly read the information data extracted from the shapefile, but I cannot get the JSON file to be well generated.

This is part of my code:

Read Data from .shp

public void esShapefileParcel(){

// ShapeFile Directory 
                String archiveshpParcel = directory+"\\"+constant.getTmpParcel()+"\\"+constant.getTmpParcel()+".shp";

                Parcel parcel = new Parcel();

            try {

                File shapeFile = new File (archiveshpParcel);
                FileDataStore store = FileDataStoreFinder.getDataStore(shapeFile);
                SimpleFeatureSource featureSource = store.getFeatureSource();
                SimpleFeatureCollection collection = featureSource.getFeatures();
                SimpleFeature fParcel;

                SimpleFeatureIterator itrParcel = collection.features();



                   fParcel = itrParcel.next();


                   ObjectMapper Obj = new ObjectMapper();
                   String jsonStr = Obj.writeValueAsString(parcels.toString());


        } catch (IOException e) {


And, my Parcel object is:

public class Parcel{

    protected String type;
    private String externalReference;


    Parcel(String type, String externalReference) {
        this.type = "parcel";
        this.externalReference = externalReference;

    public String getType() {
        return type;

    public String getExternalReference() {
        return externalReference;

    public void setExternalReference(String externalReference) {
        this.externalReference = externalReference;

    public String toString() {

             return "  \"type\" : \"" + type + "\""+
               ", \"externalReference\" : '" + externalReference + '\'' ;   


But this, only returns a invalid Json:

"[ \"type\" : \"null\", \"externalReference\" : '3545013XG2734N']" "[ \"type\" : \"null\", \"externalReference\" : '8401550XG1670S']" "[ \"type\" : \"null\", \"externalReference\" : '8401522XG1680A']" "[ \"type\" : \"null\", \"externalReference\" : '9293903XG1699C']" "[ \"type\" : \"null\", \"externalReference\" : '9293907XG1699C']"

1 Answer 1


Why not use GeoTools to handle the input and output? Then your code becomes simply something like:

File inFile = new File("/home/ian/Data/states/states.shp");
File outFile = new File("/home/ian/Data/states/states.geojson");
if (outFile.exists()) {
// Read
DataStore inputDataStore = DataStoreFinder.getDataStore(
        Collections.singletonMap("url", URLs.fileToUrl(inFile)));

String inputTypeName = inputDataStore.getTypeNames()[0];
SimpleFeatureType inputType = inputDataStore.getSchema(inputTypeName);

FeatureSource<SimpleFeatureType, SimpleFeature>
        source = inputDataStore.getFeatureSource(inputTypeName);

FeatureCollection<SimpleFeatureType, SimpleFeature>
        inputFeatureCollection = source.getFeatures();

Map<String, Object> params = new HashMap<>();
params.put(GeoJSONDataStoreFactory.URLP.key, URLs.fileToUrl(outFile));
// Write
DataStore newDataStore = DataStoreFinder.getDataStore(params);

String typeName = newDataStore.getTypeNames()[0];

SimpleFeatureStore featureStore =
        (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);



If you don't want to export all the attributes of the shapefile features you need to modify newDataStore.createSchema(inputType); to use a new limited schema. This needs something like:

SimpleFeatureType outType = DataUtilities.createSubType(inputType, new String[] {"type","REFCAT"});
  • Thanks for your advice Ian Turton, without a doubt the code is much more efficient in this way.
    – Mimmetico
    Commented Jun 11, 2019 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.