Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify a large json string?

Tags:

java

json

csv

excel

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.

like image 631
Frederik Wordenskjold Avatar asked Aug 05 '10 11:08

Frederik Wordenskjold


People also ask

How do I edit a large JSON file?

JSONBuddy provides support for huge JSON and text data (multi-GB) to view and edit those documents directly in the application. Regardless of the size of your input data, the application will only use a small amount of memory to view the file content. Open and browse big JSON text. Run well-formed syntax checking.

How do I handle a large JSON file?

Instead of reading the whole file at once, the 'chunksize' parameter will generate a reader that gets a specific number of lines to be read every single time and according to the length of your file, a certain amount of chunks will be created and pushed into memory; for example, if your file has 100.000 lines and you ...

Is there a limit to string length in JSON?

The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data.

How do I change values in JSON?

Changing key names or values If you need to change all instances of a key or value with a different one, just use the SUBSTITUTE() function. You'll need to copy and paste the value of the cell into another using Ctrl + Shift + V or Cmd + Shift + V to see the actual change in the JSON.

How do I open a 2gb JSON file?

With Gigasheet, you can open large JSON files with millions of rows or billions of cells, and work with them just as easily as you'd work with a much smaller file in Excel or Google Sheets. In one spot, the JSON data is loaded, flattened, and ready for analysis.

How do I open a large JSON file in Notepad ++?

Format JSON data using JSON viewer Once the plugin is installed, it will show up in the plugin tab. Next, you have to do is either copy any JSON data or load a JSON file by dragging it in notepad++. All data will appear in a single line. Now, JSON viewer will help you to format the data.


2 Answers

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);
like image 74
BalusC Avatar answered Oct 23 '22 09:10

BalusC


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.

like image 3
Bozho Avatar answered Oct 23 '22 09:10

Bozho