tags:

views:

122

answers:

2

Dead silence! Not often you experience that on Stackoverflow... I've added a small bounty to get things going!

I've built a json document containing information about the location of various countries. I have added some custom keys. This is the beginning of the json-file:

{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": {
"NAME": "Antigua and Barbuda", 
"banned/censored": "AG",   
"Bombed": 29, 
"LON": -61.783000, "LAT": 17.078000 },
"geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -61.686668,...

All the custom keys (like bombed, banned/censored etc.) have values, but they are just old (bogus if you want) values. The real values are kept in a .csv file extracted from a excel document.

I e.g. have this:

                            banned/censored     bombed   
Antigua and Barbuda              2                 120
...

Now I want to match these values with the proper key in the json-file. Is there any programs out there that I can use? Another option would be a json library for java, which somehow supports what I want. I havent been able to find an easy solution for it yet. The document is pretty large ~ 10MB, if it makes any difference!

EDIT: I've used QGIS to manipulate the .shp file, so some kind of extension could be of use too.

+3  A: 

Just convert both the JSON and the CSV to a fullworthy Java object. This way you can write any Java logic to your taste to alter the Java objects depending on the one or other. Finally convert the modified Java object representing the JSON data back to a JSON string.

There is however one problem in your JSON. The / in banned/censored is not a valid character for a JSON field name, so many of the existing JSON deserializers may choke on this. If you fix this, then you'll be able to use one of them.

I can recommend using Google Gson for the converting between JSON and Java. Here's a kickoff example based on your JSON structure (with banned/censored renamed to bannedOrCensored):

class Data {
    private String type;
    private List<Feature> features;
}

class Feature {
    private String type;
    private Properties properties;
    private Geometry geometry;
}

class Properties {
    private String NAME;
    private String bannedOrCensored;
    private Integer Bombed;
    private Double LON;
    private Double LAT;
}

class Geometry {
    private String type;
    private Double[][][][] coordinates;
}

You only need to add/generate getters and setters yourself. Then, you'll be able to convert between JSON and Java like follows:

Data data = new Gson().fromJson(jsonString, Data.class);

To convert between CSV and a Java object, just pick one of the many CSV parsers, like OpenCSV. You can even homegrow your own with help of BufferedReader.

Finally, after altering the Java object representing the JSON data, you can convert it back to JSON string with help of Gson as follows:

String json = new Gson().toJson(data);
BalusC
This was what I hoped to avoid. Anyway, I'll give it a try. Thanks!
Frederik Wordenskjold
What did you have in mind? Some magic?
BalusC
A program which supported some of the functionality I'm looking for. Like a an extension to QGIS, or whatever - like magic. Anyway, you got things going.
Frederik Wordenskjold
You're welcome.
BalusC
+1  A: 

While BalusC's answer tells you how to do it in your current setup, I have a more radical suggestion: get rid of the JSON.

By idea JSON is not meant to store data - it is meant to be used as a "lightweight text-based open standard designed for human-readable data interchange". That is:

  • low-traffic (as little non-meaningful data as possible)
  • human-readable
  • easy to handle with dynamic languages

Data storages on the other hand have much more requirements than this. That's why databases exist. So move your storage to a database. If you don't want a full-featured database, use something like HSQLDB or JavaDB.

Bozho
You're right. I dont like JSON myself for data storage. But while I was working with OpenLayers and .shp files, I figured it was the easiest to use format, as OpenLayers uses geojson, and .shp files can be converted to geojson through gdal.
Frederik Wordenskjold