3

I am trying to access JSON object in JavaScript file but in meantime it give me an error that Uncaught SyntaxError: Unexpected identifier. I am new to JavaScript could some please help me how to solve this problem. I will also share JSON and JS file code you may explore code there.

Main.JS

 import Lang from "./lang";

console.log(Lang["dashboard.paragraph"]);

Lang.JS

 module.exports = {
  /* 01.Dashboard */
  "dashboard.title": "Dashboard",
  "dashboard.paragraph": " Here you will put data related to Dashboard Page",
};
4
  • Where are you trying to run this? Node.js? Browser? Commented Jul 3, 2019 at 18:42
  • JSON cannot contain comments, and no trailing commas. Commented Jul 3, 2019 at 18:50
  • @David784 I am trying in browser Commented Jul 3, 2019 at 19:13
  • 1
    This is NOT a JSON object. It is a JavaScript file that exports a JavaScript object. A valid JSON file would not include module.exports Commented Jul 3, 2019 at 20:39

4 Answers 4

2
module.exports = {
  /* 01.Dashboard */
  "dashboard.title": "Dashboard",
  "dashboard.paragraph": " Here you will put data related to Dashboard Page" // removed trailing comma(,) from here
};

Remove ',' after last key value pair. You can use jsonlint or any other JSON linting tool to validate your JSON object. You are getting this error because of invalid JSON object.

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

5 Comments

Can you please make a program or do some changes in my program
Yes, the comma needs to be removed. But this is NOT a JSON object. It is a JavaScript file that exports a JavaScript object. A valid JSON file would not include module.exports.
@Intervalia Samri is trying to access JSON object from a js file not a JSON file.
He may think he is, but if the file starts with module.exports then it is not a JSON file, it is a JavaScript file. If he wan't a true JSON then then he needs to remove the comments and the module.exports = .
@Intervalia Yes and will also need to use .json instead of .js if he want to use real JSON.
1

You are mixing two module formats. One is ES6 modules and the other is CommonJS.

You also had a filename with the first letter capitalized Lang.js and importing ./lang in all lower case. The case should always match.

If you want to use ES6 modules then you need this:

Main.JS

import Lang from "./Lang";
console.log(Lang["dashboard.paragraph"]);

Lang.JS

export default {
  /* 01.Dashboard */
  "dashboard.title": "Dashboard",
  "dashboard.paragraph": " Here you will put data related to Dashboard Page"
};

And if you want to use Common JS then do this:

Main.JS

const Lang = require("./Lang");
console.log(Lang["dashboard.paragraph"]);

Lang.JS

module.exports = {
  /* 01.Dashboard */
  "dashboard.title": "Dashboard",
  "dashboard.paragraph": " Here you will put data related to Dashboard Page"
};

Comments

0

You can use a json notation on yor file and a XMLHttpRequest object to open it.

//Consider use this format to your file
let file = {
  /* 01.Dashboard */
  "dashboard.title": "Dashboard",
  "dashboard.paragraph": " Here you will put data related to Dashboard Page",
}

//here is the result
console.log(file);

//use XMLHttpRequest object to open from a file hosted with your app 
let fileOnLik;
var oReq = new XMLHttpRequest();
oReq.onreadystatechange = function ()
    {
        if(oReq.readyState === 4)
        {
            if(oReq.status === 200 || oReq.status == 0)
            {
                fileOnLik = oReq.responseText;
            }
        }
    }
oReq.open("get", "/folder/yourFile.json", true); //could be http://www.otherserver.com/yourFile.json
oReq.send();

console.log(fileOnLink);

Comments

0

I am not getting any error with your approach. Moreover, having dot(.) in object property name is not a good programming practice.

You could write your JSON as below -

module.exports = {
        /* 01.Dashboard */
      "dashboard" : {
                "title": "Dashboard",
                "paragraph": " Here you will put data related to Dashboard Page"
      }
}

1 Comment

@Samri You will also need to replace Lang["dashboard.paragraph"] with Lang.dashboard.paragraph, modify your code only if you are not bounded to use dot within keys in your object.

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.