dchester / jsonpath

Query and manipulate JavaScript objects with JSONPath expressions. Robust JSONPath engine for Node.js.
MIT License
1.34k stars 215 forks source link

AssertionError [ERR_ASSERTION]: obj needs to be an object #117

Closed patpicos closed 4 years ago

patpicos commented 5 years ago

Error:

AssertionError [ERR_ASSERTION]: obj needs to be an object
    at JSONPath.query (C:\Users\Patrick.Picard\Documents\VSCode\Acosta-Shared-JsonPatcher\node_modules\jsonpath\lib\index.js:91:10)
    at Object.<anonymous> (C:\Users\Patrick.Picard\Documents\VSCode\Acosta-Shared-JsonPatcher\app.js:20:4)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

Code:

var cities = [
    { name: "London", "population": 8615246 },
    { name: "Berlin", "population": 3517424 },
    { name: "Madrid", "population": 3165235 },
    { name: "Rome",   "population": 2870528 }
  ];

  var jp = require('jsonpath');
  var names = jp.query(cities, '$..name'); //this works.
  console.log(names);

var fs = require('fs');
var contents = fs.readFileSync('input/input2.json.txt', 'utf8');
console.log(contents);  //output looks good, matches input file

var query1 = "$.resources[?(@.type == 'Microsoft.DataFactory/factories/linkedServices' && @.properties.type == 'AzureDatabricks')]";  //Databricks Object
console.log(query1);
console.log(jp.parse(query1));

jp.query(contents,query1) //This Fails
// AssertionError [ERR_ASSERTION]: obj needs to be an object
//  at JSONPath.query (C:\Users\Patrick.Picard\Documents\VSCode\Acosta-Shared-JsonPatcher\node_modules\jsonpath\lib\index.js:91:10)

will attach the input2 file.

I have confirmed that the query works against the file using: http://www.jsonquerytool.com/

patpicos commented 5 years ago

input2.json.txt

patpicos commented 5 years ago

$ node --version v10.15.0

"dependencies": { "fs": "0.0.1-security", "jsonpath": "1.0.0" } }

djMax commented 4 years ago

This is happening to me too, and it makes so very little sense.

movitto commented 4 years ago

contents = JSON.parse(contents)

fs.readFileSync returns a string. jsonpath expects an object to query

djMax commented 4 years ago

Good point - that wasn't my issue. I had a JSON object that at the REPL said it WAS instanceof Object, but the function still threw on it. No idea why.

patpicos commented 4 years ago

not pursing more work on this. closing

ZachBien commented 2 years ago

For anyone else facing this... I did a JSON.stringify(JSON.parse(obj)) and that resolved it for me... I can't explain why.

elisherer commented 1 month ago

For anyone else facing this... I did a JSON.stringify(JSON.parse(obj)) and that resolved it for me... I can't explain why.

This loses all class types instances information (e.g. instanceof)