0

I have a archive.json file like below:

  var archive = {
  "archiveList" : [
      { url : "https://ddddd",
          greetings : "blahblahblah"},
      { url :"https://ddd1",
          greetings : "blahblah"},
       .....
  ]
}

I use the var keyword because I want to read from static page.

Here is the problem. I want to read that file and update archivelist array. How can I parse this file?

=============================Add more info================================

What I'm trying to is two things.

  1. In static page, Read json(or js object) data from static page and show up

  2. In server with node.js, Read json and insert archiveList data

What I read is this : https://stackoverflow.com/a/18637657/6234242

First thing what I'm trying to is solved, but Second one is my problem.

When I use module.exports = {...}, First thing is not working.

=============================Add more info================================

When I load and parse archive.json file, console says below.

undefined:1
 var archive = {
 ^

SyntaxError: Unexpected token v in JSON at position 1
    at JSON.parse (<anonymous>)
4
  • 1
    So is it a json or a js object? Commented Jan 15, 2019 at 9:06
  • var myJson = require('path/to/file.json'); doesn't work for you? what have you tried? Commented Jan 15, 2019 at 9:07
  • 2
    @Art3mix — Since there is no export, and it isn't JSON, that wouldn't work. Commented Jan 15, 2019 at 9:08
  • I Updated. When I use export, First thing what I'm trying to is not working Commented Jan 15, 2019 at 9:20

2 Answers 2

4

I think you're confusing some concepts here, but what you're presenting is plain JS. You can require that file if it's in your filesystem. The only real change needed is that you export something from that file:

module.exports = {
  "archiveList" : [
    { url : "https://ddddd",
      greetings : "blahblahblah"},
    { url :"https://ddd1",
      greetings : "blahblah"},
    .....
  ]
}

After that you can, for example:

const archiveList = require('./archiveList.js')

That's about it. From the little information you've provided, this should be enough, but otherwise expand on your use case.

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

2 Comments

Thank you, I expanded on my use case. When I use module.exports, first thing what i'm trying to is not working. The browser says Uncaught ReferenceError: module is not defined
Wait, are you doing this in browser or in Node?
0

Assuming the file is local, use:

module.exports = { "archiveList" : [

to export the object from the static file, then add

const list = require('./file.js')

to your application.

6 Comments

I Updated. Thank you but when I use export, First thing what I'm trying to is not working.
so its a web page not a local file, if so try this one stackoverflow.com/a/14552721/10775453
Nope file is in local, And What I'm trying to is two things. please read again
var fs = require('fs'); var obj = JSON.parse(fs.readFileSync('file', 'utf8')); for more information read: nodejs.org/api/fs.html
don't forget to replact %file% with file path, hope it helps
|

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.