Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@rajatbh777

Description

@rajatbh777
Issue body actions

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.

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.