1

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

1 Answer 1

2

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

obj: obj

be this?

data: obj
Sign up to request clarification or add additional context in comments.

2 Comments

This Worked! What does this line of code function as? i understand obj comes from the parsing of the JSON, is data: what the function is replying with?
data is the key in the array. { data: obj } means there's an array with "data" as the key and obj as the value. The function you're calling expects there to be a key called "data", so you need to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.