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

Errors in case of special characters like '#' and undefined output with square brackets while using 'JSONPath' #173

Open
rajatbh777 opened this issue Jun 27, 2022 · 2 comments

Comments

@rajatbh777
Copy link

Hi Team,

I am working on a project with Node.js and using “jsonpath-plus”. While using the module we’re facing certain issues-

Case-1: Special character ‘#’ when used in any of the keys in a json object and we try extracting it's value using “JSONPath” as shown in sample code below, it gives error as “TypeError: Cannot read property 'indexOf' of undefined”.

Case-2: In case when we use square brackets in any key i.e. ‘[’ or ‘]’, we’re not getting any error but rather the “console.log(resultobj)” statement in the above code gives “undefined” on the output window.

Case-3: While looking for different possible solutions I came to know that we’ll not get any error if we surround ‘#’ with parentheses i.e. ‘(’ and ‘)’ as shown in code below. Is this supposed to be a solution?

Case-4: We’re facing similar problems with other characters as well which are - ‘;’ , ‘^’ , ‘.’ , ‘~

Code that reproduces problem here

Code for Case-1:

const { JSONPath } = require('jsonpath-plus')
 
  function test(){
 
     let object = {
            "id!": "Hello World.",
            "Email$987": "Rajat",
            "recordType@34": "customer!",
            "Name#23": "Test customer51-128721$"            // Using '#' in a key
     }
 
     const initObj = {
           path: '$.Name#23',                              // Path with same key
           json: object,
           resultType: 'all',
           flatten: true,
           wrap: false
      }
 
     const resultobj = JSONPath(initObj)                             
     console.log(resultobj)                              //  Error - "TypeError: Cannot read property 'indexOf' of undefined"
  } 

  test()

Code for Case-2:

const { JSONPath } = require('jsonpath-plus')
 
function test(){
 
   let object = {
       "id!": "Hello World.",
       "Email$987": "Rajat",
       "recordType@34": "customer!",
       'Name[23]': "Test customer51-128721$"                // Using square brackets in key 
     }
 
   const initObj = {
       path: '$.Name[23]',                              // Path with same key
       json: object,
       resultType: 'all',
       flatten: true,
       wrap: false
   }
 
   const resultobj = JSONPath(initObj)
   console.log(resultobj)                              // Output - "undefined"  
 
}
 
test()

Code for Case-3:

const { JSONPath } = require('jsonpath-plus')
 
function test(){
 
   let object = {
       "id!": "Hello World.",
       "Email$987": "Rajat",
       "recordType@34": "customer!",
       'Name(#)23': "Test customer51-128721$"           // Using parentheses around '#'
     }
 
   const initObj = {
       path: '$.Name(#)23',                              // Path with same key
       json: object,
       resultType: 'all',
       flatten: true,
       wrap: false
   }
 
   const resultobj = JSONPath(initObj)
   console.log(resultobj)                              // No unexpected behaviour
 
}
 
test()

Console error or logs

Case-1 : TypeError: Cannot read property 'indexOf' of undefined

Case-2: undefined

Case-3:

{
  path: "$['Name(#)23']",
  value: 'Test customer51-128721$',
  parent: {
    'id!': 'Hello World.',
    'Email$987': 'Rajat',
    'recordType@34': 'customer!',
    'Name(#)23': 'Test customer51-128721$'
  },
  parentProperty: 'Name(#)23',
  hasArrExpr: undefined,
  pointer: '/Name(#)23'
}

Expected behavior

Case-1: It should not give any error.

Case-2: It should give an output as it is giving in Case-3.

Case-3: It is working as expected.

Case-4: It should not give any error and give output as in Case-3.

Expected result

Case-1:

{
  path: "$['Name#23']",
  value: 'Test customer51-128721$',
  parent: {
    'id!': 'Hello World.',
    'Email$987': 'Rajat',
    'recordType@34': 'customer!',
    'Name#23': 'Test customer51-128721$'
  },
  parentProperty: 'Name#23',
  hasArrExpr: undefined,
  pointer: '/Name#23'
}

Case-2:

{
  path: "$['Name[23]']",
  value: 'Test customer51-128721$',
  parent: {
    'id!': 'Hello World.',
    'Email$987': 'Rajat',
    'recordType@34': 'customer!',
    'Name[23]': 'Test customer51-128721$'
  },
  parentProperty: 'Name[23]',
  hasArrExpr: undefined,
  pointer: '/Name[23]'
}

Environment (IMPORTANT)

  • JSONPath-Plus version: 7.0.0

Desktop**

  • OS: macOS Monterey 12.4
  • Node Version : 14.18.1

Additional context

Please update with any solution or possible workaround.

@rajatbh777
Copy link
Author

Hi @brettz9, Can you please look into the above issue and provide any escaping logic or workaround or fix for the above issue with special characters in the JSON data key names. Thanks.

@brettz9
Copy link
Collaborator

brettz9 commented May 15, 2024

As mentioned on the main page of the README, I am not actively maintaining this project, though I am accepting PRs. Feel free to submit a well-documented PR if you find an issue and your question is not answered by the README.

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

No branches or pull requests

2 participants