jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

Filtering by keys with "-" in them?

Open oset-bschuhma opened this issue 7 years ago • 6 comments

Hi,

Trying to filter JSON like this: $..Person[?(@.-objectId=='per11282a')]

This doesn't work, but this does:

$..Person[?(@.FirstName=='HILLARY')]

and returns: [ { "-objectId": "per11282a", "FirstName": "HILLARY", "FullName": { "Text": { "-language": "en", "#text": "HILLARY CLINTON" } }, "Gender": "Female", "LastName": "CLINTON", "PartyId": "Party-1" } ]

I'm trying to find the people by their person ID, but the dash on the front of the key is causing errors.

Here's my JSON data: { "ElectionReport": { "-xmlns:ds": "http://www.w3.org/2000/09/xmldsig#", "-xmlns:err": "NIST_V1_election_results.xsd", "-xmlns": "NIST_V1_election_results.xsd", "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", "-xsi:schemaLocation": "NIST_V1_election_results.xsd file:NIST_V1_election_resultsV50.xsd",

"PersonCollection": {
  "Person": [
    {
      "-objectId": "per11282a",
      "FirstName": "HILLARY",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "HILLARY CLINTON"
        }
      },
      "Gender": "Female",
      "LastName": "CLINTON",
      "PartyId": "Party-1"
    },
    {
      "-objectId": "per11282b",
      "FirstName": "TIM",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "TIM KAINE"
        }
      },
      "Gender": "Male",
      "LastName": "KAINE",
      "PartyId": "Party-1"
    },
    {
      "-objectId": "per11284a",
      "FirstName": "GARY",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "GARY JOHNSON"
        }
      },
      "Gender": "Male",
      "LastName": "JOHNSON",
      "PartyId": "Party-5"
    },
    {
      "-objectId": "per11284b",
      "FirstName": "BILL",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "BILL WELD"
        }
      },
      "Gender": "Male",
      "LastName": "WELD",
      "PartyId": "Party-5"
    },
    {
      "-objectId": "per11286a",
      "FirstName": "GLORIA",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "GLORIA ESTELA LA RIVA"
        }
      },
      "Gender": "Female",
      "LastName": "LA RIVA",
      "MiddleName": "ESTELA",
      "PartyId": "Party-6"
    },
    {
      "-objectId": "per11286b",
      "FirstName": "DENNIS",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "DENNIS J. BANKS"
        }
      },
      "Gender": "Male",
      "LastName": "BANKS",
      "MiddleName": "J",
      "PartyId": "Party-6"
    },
    {
      "-objectId": "per11288a",
      "FirstName": "JILL",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "JILL STEIN"
        }
      },
      "Gender": "Female",
      "LastName": "STEIN",
      "PartyId": "Party-4"
    },
    {
      "-objectId": "per11288b",
      "FirstName": "AJAMU",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "AJAMU BARAKA"
        }
      },
      "Gender": "Male",
      "LastName": "BARAKA",
      "PartyId": "Party-4"
    },
    {
      "-objectId": "per11290a",
      "FirstName": "DONALD",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "DONALD J. TRUMP"
        }
      },
      "Gender": "Male",
      "LastName": "TRUMP",
      "MiddleName": "J",
      "PartyId": "Party-2"
    },
    {
      "-objectId": "per11290b",
      "FirstName": "MICHAEL",
      "FullName": {
        "Text": {
          "-language": "en",
          "#text": "MICHAEL R. PENCE"
        }
      },
      "Gender": "Male",
      "LastName": "PENCE",
      "MiddleName": "R",
      "PartyId": "Party-2"
    }
  ]
},
"SequenceStart": "1",
"SequenceEnd": "1",
"Status": "unofficial-complete",
"VendorApplicationId": "Auto-Generated v0.1"

} }

I'm using http://www.jsonquerytool.com/#/JavaScript and https://jsonpath.curiousconcept.com/ before I write it up in code, but I cannot get the query to work. Possible bug? Is there a way to escape it?

Thanks,

Bret

oset-bschuhma avatar Jan 31 '18 17:01 oset-bschuhma

Simple example with same problem

const identifiers = jsonpath.query({
  object: {
    'source-language': 'test'
  }
}, "$..source-language");

console.log(identifiers);
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: Lexical error on line 1. Unrecognized text.
$..source-language
---------^
    at Parser.parseError (./node_modules/jsonpath/generated/parser.js:166:15)
    at Parser.parser.yy.parseError (./node_modules/jsonpath/lib/parser.js:13:17)
    at Object.parseError (./node_modules/jsonpath/generated/parser.js:341:28)
    at Object.next (./node_modules/jsonpath/generated/parser.js:595:25)
    at Object.lex (./node_modules/jsonpath/generated/parser.js:605:22)
    at lex (./node_modules/jsonpath/generated/parser.js:194:28)
    at Parser.parse (./node_modules/jsonpath/generated/parser.js:207:26)
    at JSONPath.nodes (./node_modules/jsonpath/lib/index.js:118:26)
    at JSONPath.query (./node_modules/jsonpath/lib/index.js:94:22)
    at ./index.js:16:32

iamandrewluca avatar Jun 14 '18 15:06 iamandrewluca

Having the same issue, no solution so far?

nimrod-cohen avatar Sep 16 '18 07:09 nimrod-cohen

This is by design

Use '$["key-with-dashes"] not "$." For recursive query use '$..["key with spaces"]'

sdawood avatar Sep 19 '18 08:09 sdawood

Thanks @sdawood that's useful for getting at keys with a hyphen. I was surprised it worked, though, after reading the following on the JSONPath introductory post:

Currently only single quotes allowed inside of JSONPath expressions.

Nevertheless the recursion works well for me in accessing a hyphenated key via URL, i.e.

https://img.shields.io/badge/dynamic/json.svg?url=https://git.habd.as/comfusion/toxic-swamp/raw/branch/master/package.json&label=after%20dark&query=$..["after-dark"]&colorB=000000&style=for-the-badge

Given data is coming from a well-defined package manifest I was hoping for a more explicit declaration (e.g. $.dependencies.["after-dark"]) but this gets the job done. Kudos.

ghost avatar Nov 24 '18 07:11 ghost

@jhabdas If you want a better comply with the spec (single quotes), try this fork (currently a drop-in replacement) https://github.com/livereach/jsonpath

evxn avatar Jan 16 '19 19:01 evxn

This is working in Golden Path. Not sure if Golden Path supports all jsonpath capabilities.

image

ghost avatar Aug 20 '20 16:08 ghost