Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shift Spec for Root Array #106

Open
austinarbor opened this issue Jun 8, 2021 · 2 comments
Open

Shift Spec for Root Array #106

austinarbor opened this issue Jun 8, 2021 · 2 comments

Comments

@austinarbor
Copy link

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"
}]
@JoshuaC215
Copy link
Collaborator

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"}]

@austinarbor
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants