Christopher Allen Christopher Allen - 4 months ago 24
Node.js Question

Asynchronous Query JSON object

I have been playing around with a module from NPM called JSON-Query, I originally able to make the module function with JSON embedded in my js.

I have spent about two days attempting to make it query JSON that is external and in a JSON file.

The original code that was functioning looked something like this.

var jsonQuery = require('json-query')
var data = {
people: [
{name: 'Matt', country: 'NZ'},
{name: 'Pete', country: 'AU'},
{name: 'Mikey', country: 'NZ'}
]
}

jsonQuery('people[country=NZ].name', {
data: data
}) //=> {value: 'Matt', parents: [...], key: 0} ... etc


I was able to query the internal JSON to find the key I was looking for.

I realized I need the ability to update the JSON while the code is live, so I moved the JSON to its own file.

Currently my main JS file looks like this.

var jsonQuery = require('json-query');
var fs = require('fs');

function querydb(netdomain){
fs.readFile('./querykeys.json', 'utf8', function (err, data) {
if (err){console.log('error');}
var obj = JSON.parse(data);

console.log(jsonQuery('servers[netshare=Dacie2015].netdomain', {
obj: obj
}));



});
}

querydb();


My JSON file that contains the json looks like this.

{
"servers": [
{"netdomain": "google.com", "netshare": "password", "authip":"216.58.203.46"},
{"netdomain": "localhost", "netshare": "localghost", "authip":"127.0.0.1"},
{"netdomain": "facebook.com", "netshare": "timeline", "authip":"31.13.69.228"}
]
}


The issue I have ran into, I am unable to query the JSON anymore, when the function QueryDB() is ran, no matter what is in the place to query the JSON, i get no response locating my key.

Currently the response I get from the server when i try to query the JSON file is

{ value: null,
key: 'netdomain',
references: [],
parents: [ { key: 'servers', value: null }, { key: null, value: null } ] }


To be abundantly clear, i believe my issue is the way i call my object into play, i have played with the structure of the JSON-Query and have been unable to accomplish being able to isolate a key.

Any help on this would be amazing, the module that i am working with can be found on npm at [NPM]https://www.npmjs.com/package/json-query

Thank you

Answer

I think this is just a typo. Shouldn't this:

obj: obj

be this?

data: obj
Comments