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

Single vs double quotes #86

Open
diegodlh opened this issue Sep 22, 2022 · 1 comment
Open

Single vs double quotes #86

diegodlh opened this issue Sep 22, 2022 · 1 comment

Comments

@diegodlh
Copy link

In the following JSON:

[
  {
    "@context": "https://schema.org",
    "@type": "Organization",
    "name": "Organization name"
  },
  {
    "@context": "https://schema.org",
    "@type": "WebSite",
    "name": "Website name"
  }
]

I'm trying to get the name value for the first object (i.e., "Organization name"). I'm using https://jmespath.org/ but I'm posting here as I assume it uses jmespath.js.

I understand this expression won't work because @ has a special meaning: [?@type=='Organization'].author.name (it fails).

But this expression does not work either: [?'@type'=='Organization'].author.name (it returns []).

This expression does work: [?"@type"=='Organization'].author.name...

...but this one doesn't: [?"@type"=="Organization"].author.name (it returns []).

There seems to be some inconsistency regarding where double quotes and where single quotes should be used.

@springcomp
Copy link

springcomp commented Oct 20, 2022

In JMESPath expressions, you need to distinguish between identifier and raw-string literals:

  • An identifier is used to refer to the property of a JSON object.
  • A raw-string literal is a way to express a JSON string in expressions.

An identifier is usually a plain sequence of characters, without quotation marks. However, to refer to JSON properties that contain special characters, you must use double-quotation marks as you have discovered. That would be the only way to refer to the @type property.

A raw-string literal is a just a JSON string value. It is expressed as a sequence of characters surrounded with simple quotation marks.
Your first expression does not work because it tries to match the JSON string "@type" to the value of an hypothetical Organization property in your JSON objects.

Likewise your third expression does not work because it tries to match the @type property to the value of an hypothetical Organization property which does not exist.

Finally the second – correct – expression does the right thing. It tries to match the value of the property @type to the JSON string "Organization", which succeeds.

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