JSON file writing corruption with fs module - json

Since somes weeks, I am trying to create an XP System with Discord.js V13 but when I write on a JSON file with fs module new datas, I'm getting an issue in the JSON file like:
{
"status" : {
"server-id": "on"
}
}}
Somes extra brackets and getting:
JSON unexpected end
I'm using this code to write in JSON files:
​fs​.​writeFile​(​"./DataBase/xp-system.json"​,​ ​JSON​.​stringify​(​xp_system​,​ ​null​,​ ​4​)​,​ ​(​err​)​ ​=>​ ​{
​if​ ​(​err​)​ ​console​.​error​(​)​;
​}​)​;
Thanks for your help.

Related

How can I import a JSON file on Next.js on build?

I just want to do a simple thing: import a JSON file and read it within a function to return some specific data of it, but it's been really hard to do.
When on dev env, it works perfectly, but when I try to do the build, the file is not fount. It's like it's not being imported. Since I'm new with Next.JS architecture, I'm not able to find a solution by myself (read a lot of pages with help, but no success).
structure
- root
- config
- labels
data.json
index.ts
index.ts
import data from './data.json';
export type HomeLabels = typeof data.in.home;
export function getHomeLabels(language: string): HomeLabels {
return data[language as keyof typeof data].home;
}
data.json
{
"in":{
"home":{
"contact_button": "Contact",
"view_all_button": "View all"
}
},
"pt-br":{
"home":{
"contact_button": "Contato",
"view_all_button": "Ver tudo"
}
}
}
error on build
TypeError: Cannot read properties of undefined (reading 'home')
at getHomeLabels (/home/runner/work/boutme/boutme/.next/server/chunks/261.js:42:43)
All the solutions that I found out was related to using API or getStaticsProps and using on component. But I really don't want it. I think that it's possible to do what I want (import json and read just on a function), but it's been hard.

Error when trying to load local csv file in Vue3 app

I am trying to load a local CSV file with papaparse to extract it's content in a Vue3 application.
When I try something like:
let myTest = Papa.parse("#/assets/data/postcodes.csv", {
delimiter: ",",
header: true
});
I get a blank response as if the filepath is wrong. However, if I try importing the file directly with Vue3, so I can pass it to the papaparse method, like this:
import myFile from "#/assets/data/postcodes.csv"
...
let myTest = Papa.parse(myFile, {
delimiter: ",",
header: true
});
I get this error:
ERROR in ./src/assets/data/postcodes.csv 3:5
Module parse failed: Unexpected token (3:5)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| Suburb,Postcode
| Condobolin,2877
> Lake Cargelligo,2672
| Murrin Bridge,2672
| West Wyalong,2671
What is the correct way to reference a local csv file in the asset directory? I assume my file path is correct because of the error.
It hasn't been an issue loading other local assets like images and icons.

How to add path to export my data to file .csv

I am using lib in npm for project angular call json2csv for export JSON to file.csv (excel)
Here is my code:
const fields = ['id', 'name'];
const opts = { fields };
try {
const parser = new json2csv.Parser(opts);
const csv = parser.parse(this.data);
console.log(csv);
} catch (err) {
console.error(err);
}
Printing object is correct data but not create file.
Can anyone help me when add filename and path in my code?
The json2csv node_module that you're trying to use in your Angular App is not supposed to be used there. It's supposed to be used on your NodeJS backend.
Your Frontend/Client is not responsible for writing files to the system. Your Backend/Server is.
Ideally, you should be creating a REST API to which, you'll be passing the JSON to be written in a CSV file as a Request Payload.
Your NodeJS Backend can then respond to that request with the downloadable CSV file that is generated by using the json2csv node_module

Unable to print complete JSON using console.log

I am trying to import JSON file into Sample variable but only first few characters are displayed from Sample variable.
The sample.json is 20,00,000 characters, When i print Sample variable on Console only first 3,756 characters are printed.Is there any limitations on the characters that can be printed through console.log?
Complete data persists in Sample variable, I verified it by searching for strings that occur at the end of sample.json file
var Sample = require('./sample.json');
export default class proj extends Component {
constructor(props) {
super(props);
this.state = {
locations: [],
};
}
loadOnEvent() {
console.log(Sample);
//this.state={ locations : Sample };
}
}
Is there any other way to print data in Sample variable.
You have to convert json to string using JSON.stringify before logging.
/* ... */
loadOnEvent() {
console.log(JSON.stringify(Sample));
//this.state={ locations : Sample };
}
/* ... */
Try to use another way to load. Use fetch if file is remote or use fs if file is local.
If it is memory problem supposed by #Shota consider to use server side processing requests to json file. It is good solution to setup microservice which load json file at startup and handle requests to data struct parsed from json file.
Answer for webpack use case:
Configure webpack to use file-loader or copy-webpack-plugin for specifically this file because it enough big. Consider to load it in parallel with webpack bundle. If your application have big parts which need not each case they must be moved to separated bundles.

Is there a limit for array size in QJson?

I am working with QT 5.5 and I get a json with QNetwork and I try to parse it.
The json object is like this :
{
"cas_version":1008,
"result":"true",
"diaporamas":
{
"0":
{
"diaporamaid":5,
"diaporamamaj":1440433023,
"slides":
{
"0":{"slideid":32,"slidecontent":"html content here"}
}
}
}
If i run my program with this, no problem, the json is correctly parsed.
But if i have more than 10 elements inside slides, i get this error :"illegal value"
The json is generated by PHP5.5
Thank !
Thank you very much, it was not an issue in the json part (which is missing a "}"). The problem was when Qt downloading the file, the file was corrupted (I open the buffer too early)