kazaam icon indicating copy to clipboard operation
kazaam copied to clipboard

Shift Spec for Root Array

Open austinarbor opened this issue 3 years ago • 2 comments

Is it possible to use shift when the root is an array?

Given the json

[{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
	"streetAddress": "naist street",
	"city"         : "Nara",
	"postalCode"   : "630-0192"
},
"phoneNumbers": [
	{
		"type"  : "iPhone",
		"number": "0123-4567-8888"
	},
	{
		"type"  : "home",
		"number": "0123-4567-8910"
	}
]
}]

And the spec

[{"operation":"shift","spec":{"[*].streetAddress":"[*].address.streetAddress"},"require":false}]

The result is

{"[*]":{"streetAddress":null}}

It should be

[{
 "streetAddress":  "naist street"
}]

austinarbor avatar Jun 08 '21 23:06 austinarbor

Hi @austinarbor, the right way to handle this is with the "over" keyword, although this was not totally hooked up before. I believe this will work with the latest release v3.4.9. Kazaam generally was built with the assumption that the input is a JSON object rather than a top level array so I'm not sure whether this will work in all cases, but with the fix it does seem to work for the case you described.

Please confirm whether this now works for your use case, and drop a new issue if you notice any further weirdness for other cases. Here's what I did locally after the fix:

$ cat ~/spec.json 
[{"operation":"shift","spec":{"streetAddress":"address.streetAddress"},"require":false,"over":"$"}]
$ cat ~/doc.json
[{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
	"streetAddress": "naist street",
	"city"         : "Nara",
	"postalCode"   : "630-0192"
},
"phoneNumbers": [
	{
		"type"  : "iPhone",
		"number": "0123-4567-8888"
	},
	{
		"type"  : "home",
		"number": "0123-4567-8910"
	}
]
}]
$ ./kazaam -spec ~/spec.json -in ~/doc.json
[{"streetAddress":"naist street"}]

JoshuaC215 avatar Jun 10 '21 03:06 JoshuaC215

@JoshKCarroll thanks! this looks good. i did have some other observations but i'll open another issue for those

austinarbor avatar Jun 11 '21 14:06 austinarbor