Christopher Allen Christopher Allen - 1 year ago 73
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



My JSON file that contains the json looks like this.

"servers": [
{"netdomain": "", "netshare": "password", "authip":""},
{"netdomain": "localhost", "netshare": "localghost", "authip":""},
{"netdomain": "", "netshare": "timeline", "authip":""}

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]

Thank you

Answer Source

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

obj: obj

be this?

data: obj
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download