question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding a new Field in array elements

See original GitHub issue

Hi,

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:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
mhubbardcommented, Aug 9, 2016

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

[
  {
    "operation": "modify-default-beta", 
    "spec": {
      "customer": {
        "*": {
          "locations": {
            "*": {
              "customId": "@(1,id)"
            }
          }
        }
      }
    }
  }
]

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:

[
  {
    "operation": "shift", 
    "spec": {
      "customer": {
        "*": {
          "locations": {
            "*": {
              "id": "customer[&3].locations[&1].id",
              "id": "customer[&3].locations[&1].customId", 
              "name": "customer[&3].locations[&1].name"
            }
          }
        }
      }
    }
  }
]
0reactions
mhubbardcommented, Apr 5, 2017

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

writers = Arrays.asList( TRAVERSAL_BUILDER.build( ((String)rhs).replaceAll("\\s+","") ) );

And lines 137 and 140 become the following

return FunctionArg.forContext( TRAVERSAL_BUILDER.build( arg.substring( 1 ).replaceAll("\\s+","") ) );
return FunctionArg.forSelf( TRAVERSAL_BUILDER.build( arg.replaceAll("\\s+","") ) );

This is just for the right hand side though, the keys/lhs requires similar changes to parent class of the *LeafSpec classes.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found