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

Why are literals left stripped? #212

Open
tomelliff opened this issue Oct 21, 2020 · 2 comments
Open

Why are literals left stripped? #212

tomelliff opened this issue Oct 21, 2020 · 2 comments

Comments

@tomelliff
Copy link

tomelliff commented Oct 21, 2020

I was confused by the behaviour of the following:

{
  "locations": [
    {"name": "Seattle", "state": "WA"},
    {"name": "New York", "state": "NY"},
    {"name": "Bellevue", "state": "WA"},
    {"name": "Olympia", "state": "WA"}
  ]
}

when queried with the following JMESPath expression:

locations[].{"CityState": join(` - `, [name, state])}

outputs the following:

[
  {
    "CityState": "Seattle- WA"
  },
  {
    "CityState": "New York- NY"
  },
  {
    "CityState": "Bellevue- WA"
  },
  {
    "CityState": "Olympia- WA"
  }
]

Note that this also occurs on the jmespath.org website so is presumably not just a Python implementation issue.

I noticed 4c87771#diff-ff12e4d40b1d1e1df346464bf6958de94c760d06acc3fce591b6ccf94e3b9449R137 adding the lstrip here but there's no explanation as to why that I can see and I can't see anything in the spec. I also didn't see anything jump out at me from a very quick look at the Go implementation but it's also a bit trickier to follow there.

@alk-acezar
Copy link

  1. it may be better to join strings with strings: locations[].{"CityState": join(' - ', [name, state])}
  2. I don't know what is happening but it's a bit more complicated than just stripping. With locations[].{"CityState": join(`true`, [name, state])} you'll get an error.

@springcomp
Copy link

springcomp commented Nov 9, 2022

@tomelliff JSON literals are there to output valid JSON and whitespace is allowed but ignored when emitting the resulting JSON. That is why they are stripped.

Your expression uses deprecated syntax which is no longer valid JMESPath since JEP-12 Raw String Literals have been introduced. Because it will try and interpret ` - ` as JSON - which is not valid.

@alk-acezar is right, you MUST use raw-string literals instead in your expression.

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

3 participants