Looks like you have poorly formed JSON.
Paste it here to see if it validates: https://jsonformatter.curiousconcept.com/
Chances are that it is whatever comes before that comma.
Answer from Korgrue on Stack OverflowLooks like you have poorly formed JSON.
Paste it here to see if it validates: https://jsonformatter.curiousconcept.com/
Chances are that it is whatever comes before that comma.
If you want to create a JSON with a set of objects, you need to create an array to wrap the objects, so I suppose this should solve your problem:
[
{"id" : "AAFS", "department" : "Africana Studies"},
{"id" : "AANT", "department" : "Anthropology"},
{"id" : "AARA", "department" : "Arabic"},
{"id" : "AARH", "department" : "Art History"},
{"id" : "AART", "department" : "Art"}
]
javascript - Import JSON and convert to array - Stack Overflow
javascript - Importing .json into Typescript as an array - Stack Overflow
how to import a json file into Javascript?
html - How to import an array file into a javascript file - Stack Overflow
One way to import the default export is this syntax
import data from './list.json';
const array = data;
console.log(array);
But you should also be able to access the array elements with your import method:
import * as data from './list.json';
const array = data;
console.log(array[1]); // 'Element 2'
There are a few ways you can import json.
import json from "file.json";
import json = require("file.json");
import * as json from "file.json";
You can use whichever you want. The first one is preferred.
So i am using the fetch method to import my local json file into my javascript program and then assigning it to my variable `data`
let data = fetch("./data.json")
.then(response => {
return response.json();
});
console.log(data)this is the code I am using but it is giving the console value
Promise { <state>: "pending" }
β
<state>: "fulfilled"
β<value>: Array(6) [ {β¦}, {β¦}, {β¦}, β¦ ]
ββ
0: Object { title: "Work", timeframes: {β¦} }
ββ
1: Object { title: "Play", timeframes: {β¦} }
ββ
2: Object { title: "Study", timeframes: {β¦} }
ββ
3: Object { title: "Exercise", timeframes: {β¦} }
ββ
4: Object { title: "Social", timeframes: {β¦} }
ββ
5: Object { title: "Self Care", timeframes: {β¦} }
ββ
length: 6
ββ
<prototype>: Array []
β
<prototype>: Promise.prototype { β¦ }but i just want that value part that is an array, cause i want to use those values with my for loop, can i get some help
dummyData.js
export const data = [{}, ......, {}];
main.js
import { data } from './dummyData';
if you are using vanilla js, without es6 features, you could do the following:
//dummyData.js
module.exports = [{} ,........, {}];
//main.js
var data = require('./dummyData');
you should first export your json/array from a file
file should be something like
export const myJsonArray = [{...}, {...}, ...]
then in your main.js you can import the jsonArray like this
import myJsonArray from "{file_path}"
As long as the file is inside your project folder (config file, i.e.) you can load it synchronously requiring it directly in NodeJS.
var test = require('./array.json');
And then the content will be loaded into your variable in the next executed sentence.
You can try to console.log it, and it will print:
[ { name: 'c', content: '3', _prototype: 'item' },
{ name: 'd', content: '4', _prototype: 'item' } ]
Right exactly in the order that the file had.
fs.stat is async, so your function is async.
You want fs.fstatSync instead.
Since you're storing as JSON extension you should not use export const data = ...
You should directly store the data
xyz.json
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
]
JSON
For demo you can see this
Or you can set extension to .js
export const data = ...some data
at required place
import {data} from '../filepath'
One workaround for the default HTML standard prohibition of importing files not served as */javascript is to use fetch() to get the file, then export the result as default and import a data URL
<script type="module">
(async() => {
let {default:sample} = await import(`data:application/javascript,${encodeURIComponent('export default ')}${JSON.stringify(await(await fetch('file.json')).json())}`);
console.log(sample);
})()
</script>
plnkr
See also ES6 modules in local files - The server responded with a non-JavaScript MIME type