JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

rajatbh777 opened this issue · comments

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.