jmespath / jmespath.py

JMESPath is a query language for JSON.

Home Page:http://jmespath.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why are literals left stripped?

tomelliff opened this issue · comments

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.

  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.

@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.