-2

I have a JSON file similar to the following:

[
  "thing1",
  "thing2",
  "thing3",
]

Let's call this file some-things.json.

I have a JavaScript file in the same directory as this JSON. I need the JSON data to be read in as an array, used elsewhere in the JavaScript.

What's the best way to do this? Do I have to read the JSON into a string and then parse to an array, or is there a nicer way? For example, array = ReadJSONArray('some-things.json').

I have looked for duplicates, but I haven't found any questions where the JSON is in a file, not a string.

Also, as stated in the question title, I don't want to use JQuery for this. I have seen a few questions on SO where people have asked this question, but tagged as and/or mentioning JQuery.

James Vickery
  • 732
  • 3
  • 10
  • 23
  • that does not look like a json – Flying Gambit Sep 08 '16 at 11:12
  • 1
    read this article, it may help you load the json file in your script: https://codepen.io/KryptoniteDove/post/load-json-file-locally-using-pure-javascript – andrew Sep 08 '16 at 11:14
  • @FlyingGambit care to elaborate? – James Vickery Sep 08 '16 at 11:14
  • @James — Try to parse it. See the syntax error. – Quentin Sep 08 '16 at 11:16
  • this may help https://codepen.io/KryptoniteDove/post/load-json-file-locally-using-pure-javascript – Sachin Gupta Sep 08 '16 at 11:17
  • @James I thought JSON is a collection name/value pairs. I understand now that it can even be a collection values. http://www.json.org/ – Flying Gambit Sep 08 '16 at 11:18
  • @Claies The accepted answer for that question is JQuery. Also, I'm not running this JavaScript in a browser – James Vickery Sep 08 '16 at 11:21
  • @FlyingGambit Yes, I've tested this JSON and it parses successfully – James Vickery Sep 08 '16 at 11:24
  • I assume by that comment "not running this JavaScript in a browser" that you are trying to do this in node? you might get better responses if you make that distinction, since node has a very specific way of dealing with the file system. – Claies Sep 08 '16 at 11:25
  • @Claies Sorry I didn't realise it was relevant, my mistake. The JavaScript is being called from grunt – James Vickery Sep 08 '16 at 11:27
  • in grunt, you have `array = grunt.file.readJSON('some-things.json');`. does this do what you are needing? – Claies Sep 08 '16 at 11:30
  • @Claies unfortunately not, I'm running karma, and need the array of files in karma config, which is a .js – James Vickery Sep 08 '16 at 11:34
  • what about this then? http://stackoverflow.com/questions/22003472/how-to-load-external-json-file-using-karmajasmine-for-angularjs-testing – Claies Sep 08 '16 at 11:38
  • unfortunately, your question still feels unanswerable, since you didn't really ask what you want to do, with the tools you want to use; JavaScript is unfortunately not specific enough, and describing the tools you *don't* want to use isn't enough to explain what you *do* want to use. – Claies Sep 08 '16 at 11:40

2 Answers2

-1

For reference, I solved this by using:

var array = require('./some-things.json');
James Vickery
  • 732
  • 3
  • 10
  • 23
-3

Do I have to read the JSON into a string and then parse to an array…?

In general, yes.

XHR allows you to specify a response type of json, which lets you read the file over XHR and parse it in one go, but support is not universal.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335