Ladislav, as the founder and manager at Seges Ltd., is heavily interested in open-source community trying to spread knowledge about Java technologies, especially Google Web Toolkit. Participating in several open-source projects and organizing local meetups and discussions he believes in broader collaboration and increasing code quality among local and also abroad developers. Ladislav has posted 8 posts at DZone. View Full User Profile

JSON data migration

09.09.2011
| 6194 views |
  • submit to reddit
JSON data format is simple and still powerful. Nowadays you can encounter more and more web applications communicating using JSON format then a couple of years ago. It is simple for a developer to read the format, it is effective for a web browser to parse the format and there are databases using it as its primary data format.

But what happens when the data structure changes? You need to migrate.

And that is where acris-json-migration might help you!

Example situation

Let's shed a light into it and assume we have a data like this:

{
        "firstName":"John"
        "secondName":"Doe"
        "street":"Over the rainbow"
        "streetNr":21
}

 

Such data can be represented by following Java domain object:

public class Person {
        String firstName;
        String secondName;
        String street;
        Integer streetNr;

        // ... and getters and setters...
}

 

Well, this seems like data about a person named John Doe. We stored it in a database and you can clearly see, that secondName is probably not the field name we really like to have. But a developer made a mistake and in second version of our domain model we are going to fix it:

{
        "firstName":"John"
        "surname":"Doe"
        "street":"Over the rainbow"
        "streetNr":21
}

 

Now you can see the point - thousands of data stored in the format defined by Person class in its version #1 but our program communicating in version #2 with changed secondName to surname in Person class. Clients can wonder why the don't see surnames, can't they? ;)

One thing to remember (for the following context) - the class Person changed and there is only Person class in version #2.

Simple migration script

In this situation I would like to write a script:

public class PersonV1toV2Script extends JacksonTransformationScript<ObjectNode> {

        @Override
        public void process(ObjectNode node) {
                rename(node, "secondName", "surname");
        }

}

From the above example it is clear that the script will do the job. And you can do pretty anything with the whole tree of JSON data - adding new nodes, removing existing ones, transforming here and there - all thanks to Jackson's tree model.

How can I execute it?

There is a Transformer abstract class representing a transofmer responsible for passing JSON data to a script and writing it back.

Currently there are tow kinds of transformers:

Jackson-based is the preferred one and is more developed then JSONT-based.

So to execute a transformation on a data set you have to specify only two lines of code:

JacksonTransformer t = new JacksonTransformer(input, output);
t.transform(PersonV1toV2Script.class.getName());

... where input and output represent directories. In the input directory all files are treated as files containing JSON data and are transformed and written to the output directory. For a detailed test you can look into TransformerTest in the project.

Conclusion

The script's helper API is evolving and provides you with nice methods like removeIfExists or addNonExistent methods. We would like to hear about your use-cases which are not handled by acris-json-migration yet so the project can generally serve the purpose of JSON data migration.

Published at DZone with permission of its author, Ladislav Gažo.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Fabrizio Giudici replied on Fri, 2011/09/09 - 9:51am

Excellent. Do you have experience that it supports even more complex transformations? I'm not an XML hater, but in some cases I'd like to use JSON in place of it - and one of the common reasons I've stayed so far with XML is migration, and the fact that in XML you can do it with XSLT. If JSON has an equivalent tool, there's one less reason for XML.

Ladislav Gažo replied on Sun, 2011/09/11 - 9:11am

Yes Fabrizio, that is the reason why I created the project. You can do pretty anything within the transformation script. Currently there are only few helper methods but you can still use Jackson's tree model methods.

I will write follow-up article about integrating it within a server, so there it will be more clear how to proceed in enterprise environment.

Fredo Derazin replied on Tue, 2011/09/13 - 1:47pm

Wow! excellent post buddy. I love Json personally for the simply fact that I can transfer data back and forth without having to refresh the browser. I think you've taken it a step higher by employing OOP approach with it. Good job!

internet marketing

John David replied on Thu, 2012/01/26 - 3:20am

JSON data format is simple and still powerful. Nowadays you can encounter more and more web applications communicating using JSON format then couple of years ago. It is simple for a developer to read the format, it is effective for web browser to parse the format and there are databases using it as its primary data format.

But what happens when the data structure changes?

Java Eclipse

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.