2

I'm working on a small server (NodeJS environment) to get JSON data from a URL and a local file to compare these.

I'm using 'get-json' to load the JSON form the url and get some values, unfortunately it doesn't work for the local file.

I tried it with 'request' too.

var getJSON = require('get-json')
var localpath = "./location/file.json"

getJSON(localpath, function(data, allowed_content) {
  var a = allowed_content[0].age;
  var b = allowed_content[1].age;
  var c = allowed_content[2].age;
  var d = allowed_content[5].age;
  var e = allowed_content[6].age;
});

I can't figure it out why the local path isn't working.

5
  • Are you trying to do it in browser or in node.js environment? Commented Jul 29, 2019 at 10:06
  • Possible duplicate of JQuery.getJSON() reading a local file Commented Jul 29, 2019 at 10:07
  • In NodeJS environment Commented Jul 29, 2019 at 10:07
  • 1
    Read this : stackabuse.com/reading-and-writing-json-files-with-node-js Commented Jul 29, 2019 at 10:08
  • Just FYI, I removed the local-storage tag, because that refers to the localStorage object in browsers. Commented Jul 29, 2019 at 10:09

3 Answers 3

1

You can simply use fetch function

var localpath = "./location/file.json"
    fetch(localpath)
    .then(response => response.json())
    .then(json => {
      console.log(json);
    }); 
Sign up to request clarification or add additional context in comments.

Comments

0

tl;dr don't use get-json to read local files but use readFile

get-json is for network request, its docs says it wraps around requests in node, and uses JSONP on the browser. To read a local file just use readFile or readFileSync:

var { readFileSync } = require("fs");
var localPath = "";
var localFile = readFileSync(localPath, "utf8"); // utf8 is opts and depends on your setup, the async version of readFile takes a callback as last argument
console.log(JSON.parse(localFile));

4 Comments

It's working, now I need to figure out how I can get the specific values from the value. like "allowed_content[6].age"
Not sure I follow you. Calling JSON.parse on a string should return a JS object (or throw an error). Name the output allowed_content and you can access all its properties using regular syntax
I mean, I need specific values from the JSON file. I'll try to figure it out. Thanks again for your help!
Ah, good luck with this. If you have a memory limit issue you might be interested in oboejs.com wich is a json streaming library. If my answer is OK can you accept it ?
0

The solution was real easy actually:

var myvar = require('./filepath/myfile.json');
var a = myvar[0].age;
var b = myvar[1].age;
var c = myvar[2].age;
var d = myvar[5].age;
var e = myvar[6].age;

Comments

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.