Reputation: 1654
I am getting a null value for my transformed object when trying to convert something like this:
{
"employees": [
{ "f_name" : "tom", "l_name" : "smith" },
{ "f_name" : "don", "l_name" : "jones" }
]
}
to this:
{
"employees": [
{ "firstName" : "tom", "lastName" : "smith" },
{ "firstName" : "don", "lastName" : "jones" }
]
}
This is the spec I am using:
[
{
"operation" : "shift",
"spec" : {
"employees" : {
"f_name" : "firstName",
"l_name" : "lastName"
}
}
]
This is the code I am using:
List<Object> chainrSpecJSON = JsonUtils.classpathToList("path/spec.json");
Chainr chainr = Chainr.fromSpec(chainrSpecJSON);
Object inputJSON = JsonUtils.classpathToObject("path/input.json");
Object transformed = chainr.transform(inputJSON);
System.out.println(transformed);
I was able to successfully transform the following input with the same spec and code as above:
{
"employees":
{ "firstName" : "tom", "lastName" : "smith" }
}
So what do I need to do to transform an array of employee objects?
Upvotes: 12
Views: 20900
Reputation: 65105
One option would be generating new keys by copying the existing ones through use of a modify transformation, and the get rid of the originals by using remove transformation spec such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"employees": {
"*": {
"firstName": "@1,f_name",
"lastName": "@1,l_name"
}
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"*": {
"*_name": "" // common key identifier
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Upvotes: 0
Reputation: 4586
This spec does what you want
[
{
"operation": "shift",
"spec": {
"employees": {
"*": {
"f_name": "employees[&1].firstName",
"l_name": "employees[&1].lastName"
}
}
}
}
]
The key thing is you need to use a "*"
to loop thru all the elements of the employees
array, then when you recurse / match down to f_name
and l_name
, you need to reference the index array using [&1]
, which means look up the tree two levels, zero then one, and use that as an index array in the output.
Upvotes: 20