Adding a new Field in array elements
See original GitHub issueHi,
We were struggling to copy and add a new element in case of array,
Example : Input :
{
"customer": [
{
"locations": [
{
"name" : "Location1",
"id": 1234
},
{
"name" : "Location2",
"id": 4567
}
]
}
]
}
Expected Output :
{
"customer": [
{
"locations": [
{
"name" : "Location1",
"id": 1234,
"customId" : 1234,
},
{
"name" : "Location2",
"id": 4567,
"customId" : 4567
}
]
}
]
}
Here “customId” is a field which is a copy of “id” field in the array elements. We Tried the following spec:
[
{
"operation": "shift",
"spec": {
"customer": {
"*": {
"locations": {
"*": {
"id": "customer[&1].id",
"name": "customer[&1].name",
"id": "customer[&1].customId"
}
}
}
}
}
}
]
But couldn’t achieve it. We were expecting not to touch other fields in spec(as the input json will be a very huge file) and achieve the final output only adding the info about the “customId” field in the spec.
Any help is very much appreciated.
Issue Analytics
- State:
- Created 7 years ago
- Comments:6
Top Results From Across the Web
How to add an element containing fields to an array
Using javascript, how can I add to an array an element which contains fields (pairs of field name and field value)?
Read more >JavaScript Array Insert - How to Add to an Array with the Push ...
The first and probably the most common JavaScript array method you will encounter is push(). The push() method is used for adding an...
Read more >Adding a new field to a struct array - MATLAB Answers
Hey All! I'm trying to add a new field to a struct array inline. ... If you have a cell array of contents...
Read more >Add new field to each existing array element of existing ...
I'd like to use N1QL via the query editor to add a field to documents that match a specific criteria . Those documents...
Read more >How to add an object to an array in JavaScript - GeeksforGeeks
The push() method is used to add one or multiple elements to the end of an array. It returns the new length of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There’s a few issues with your spec, however the main one is that you have a json object that has duplicate keys of “id”, when jackson tries to transform your spec into a java object to be fed into the jolt engine it writes the first id key with a value of “customer[&1].id” then overwrites it with “customer[&1].customId”.
Luckily what you’re trying to do can be done with a the default operation which will avoid the duplicate keys
Just for future reference your shift operation wasn’t fully recreating your tree, you needed to add the locations path to the right hand side as well, and traverse 2 levels higher for the right customer array index. If the duplicate key wasn’t an issue would would be able to do the shift like this:
I too have spent some time on errors like that, that request should probably go in it’s own issue though. It’s not too hard to fix, just remove the whitespace before passing to the traversal builder in each *LeafSpec.java
For instance line 62 in ShiftrLeafSpec.java becomes
And lines 137 and 140 become the following
This is just for the right hand side though, the keys/lhs requires similar changes to parent class of the *LeafSpec classes.