Jan
Jan

Reputation: 965

Java tool to reformat JSON keys

I'm looking for a tool that allows easy re-formatting of JSON objects. Specifically, I'd like to be able to specify a set of JSON keys to turn into other keys. The difficulty comes in that I'd like to be able to change the hierarchical structure of the JSON.

As an example, it would be awesome if I could reformat something along the lines of this:

json1 = {
    "key1" : "val1",
    "key2" : "val2",
    "key3" : {
        "key4" : "val3",
        "key5" : "val4",
        "key6" : {
            "key7" : "val5",
            "key8" : "val6"
        }
    }
}

into

json2 = {
    "Key1" : "val1",
    "Key2" : "val2",
    "Key4" : "val3",
    "Key5" : "val4",
    "Advanced" : {
        "key7" : "val5",
        "key8" : "val6"
    }
}

just by doing something like this:

JsonKeyMapper mapper = new JsonKeyMapper();
mapper.addMapping("key1", "Key1");
mapper.addMapping("key2", "Key2");
mapper.addMapping("key3.key4", "Key4");
mapper.addMapping("key3.key5", "Key5");
mapper.addMapping("key3.key6.key7", "Key7");
mapper.addMapping("key3.key6,key8", "Key8");
mapper.doMapping()

That is, I'd like to be able to specify the whole hierarchical mapping of old JSON keys to new JSON keys, not just a flat dictionary-type list.

I know I can code this on my own if necessary. However, I also know that it'll take several hours, and I'd rather avoid the headache if there's a readily available tool out there.

Upvotes: 2

Views: 77

Answers (3)

Milo S
Milo S

Reputation: 4586

This Jolt spec does the transform you listed.

[
    {
        "operation": "shift",
        "spec": {
            "key1": "Key1",
            "key2": "Key2",
            "key3": {
                "key4": "Key4",
                "key5": "Key5",
                "key6": {
                    "key7": "Advanced.key7",
                    "key8": "Advanced.key8"
                }
            }
        }
    }
]

Can try it at http://jolt-demo.appspot.com/

Upvotes: 2

Manohar
Manohar

Reputation: 127

Yeah use JOLT. It is a json to json transformation completely developed in java. You just need to write a spec file for transformation. You can write your custom transformations specific to you also.

Upvotes: 0

John Harris
John Harris

Reputation: 332

Check out Jolt. It's sort of an XSLT equivalent for JSON.

Upvotes: 0

Related Questions