I've created json file using Visual studio:
{
"test": "asd"
}
Using this code to read it:
var test = fs.readFileSync('./files/test.json')
var obj = JSON.parse(test);
which results in error: Unexpected token in JSON at position 0
When I try to read package.json, it is read correctly. Does anyone know why I can't read my file?
You have 2 options
add encoding option
var test = fs.readFileSync('./files/test.json', {encoding: 'utf8'})
var obj = JSON.parse(test);
If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.
require json
var obj = require('./files/test.json');
As of node v0.5.x yes you can require your JSON just as you would require a js file.
I hope this code help u
$.getJSON("/files/test.json", function(json) {
alert(json['test'])
//if massage show 'object object' then firs purse
//json=JSON.parse(json) //alert(json['test']) });
Solution of the problem is opening file in notepad++ and saving it without BOM. Looks like json created via visual studio adds BOM
It looks like this is not properly formatted JSON. They modifying it like this.
var myObject = {
'test': 'asd'
};
and then you parse would be....
var obj = JSON.parse(myObject);
Related
I created an array of strings and placed them into an array called schoolsArray. I want to be able to create a text file for each school, using fs.
For some reason, I just can't get this to work. It seems that the issue is with using the value from schoolsArray[0] as a string in the path name. Please take a look at my series of tests. This first code snippet all works, but I added it just to help you understand that I import 'fs' and create a directory first.
Update - Added schoolArray creation per request
var fs = require('fs');
// Read all schools into array (read from text file)
const schoolFile = "./assets/dispatch/state/schools/county_name.txt";
fileInput = fs.readFileSync(schoolFile, "utf-8");
const schoolArray = fileInput.split("\n");
// Variable for chat logs directory
const chatDir = "./chat-logs";
// Create directory if it doesn't exist
if(!fs.existsSync(chatDir)){
fs.mkdirSync(chatDir);
}
The directory is created, now try make a file attempt #1
var schoolTextFile = chatDir + "/" + schoolArray[0] + ".txt";
fs.writeFileSync(schoolTextFile, "");
Uncaught Error: ENOENT: no such file or directory, open 'C:\Users\PC\Desktop\Node_Webkit_Test\chat-logs\Test School Name.txt'
at Object.fs.openSync (fs.js:653:18)
at Object.fs.writeFileSync (fs.js:1300:33)
Okay, so that doesn't work for some reason. Attempt #2 - I have come to think that the schoolArray[0] value isn't being read as a string, so I tried this:
var schoolTextFile = chatDir + "/" + toString(schoolArray[0]) + ".txt";
fs.writeFileSync(schoolTextFile, "");
There are no errors here, but the output is an undefined object:
Attempt #3 was to simply try a text string instead of using the value from my array. This worked exactly as intended.
var schoolTextFile = chatDir + "/" + "some Text 1234" + ".txt";
fs.writeFileSync(schoolTextFile, "");
Thus, the issue is pinpointed to be with the schoolArray[0] value being entered into the path. It seems silly to even test, but I did this anyway...
var somestring = "some text 1234";
console.log(typeof somestring);
// The log says that this is a string.
console.log(typeof schoolArray[0]);
// The log says that this is a string.
So then, why does one string work here, and the other causes path issues? Thanks in advance!
You must have some forbidden characters in schoolArray. Typically \r. Try
schoolArray = fileInput.split("\n").map( line => line.replace(/\r/g,''));
I have a file.txt that I need to get to my script and parse via d3.request.
The content of file is encoded with windows-1250 encoding and has extra lines to be deleted, so that only lines starting with 'Date' and '2017' should pass.
So far I have been using cli solution to grep text file (removing extra lines) and use d3 dsv2json to get clean json which can be loaded.
$ grep -E '^(Date|2017)' file.txt > file.csv
$ dsv2json -r ';' --input-encoding windows-1250 --output-encoding utf-8 < file.csv > file.json
However now i need to do these operations programmatically once txt file is loaded in the script via d3.request.
d3.request('file.txt')
.mimeType('text/csv')
.response(function(response) {
// response.responseText
})
TheresponseText gives me raw data with wrong encoding and extra lines. How to fix it so it will produce clean json at the end?
After further investigation I have found solution.
To decode file I used solution from here with TextDecoder. In order to do this d3.request.response should be set to arraybuffer.
function decode(response) {
const dataView = new DataView(response);
const decoder = new TextDecoder("windows-1250");
const decodedString = decoder.decode(dataView);
return decodedString
}
To filter out extra lines I used following step:
function filterData(rawData) {
return rawData
.split(/\n/)
.filter(row => (row.startsWith('Data') || row.startsWith('2017')))
.join('\n')
}
So finally, in context of d3.request:
d3.request('file.txt')
.header('Content-Type', 'text/csv;charset=windows-1250')
.mimeType('text/csv')
.responseType('arraybuffer')
.response(function(xhr) {
const decoded = decode(xhr.response)
const filtered = filterData(decoded)
const json = d3.dsvFormat(';').parse(filtered)
return json
})
.get()
I want to send some string data from Python3 to nodeJs. The string is Korean characters and I am encoding it to utf8.(Cause I don't know other ways sending data safely.) When I send(from python) it is ByteStream and in nodeJs I receive it as Array. I convert this Array to String. But now I cannot decode string back to original Korean characters.
Here are some codes I am using.
python
input = sys.argv[1]
d = bot.get_response(input)
data = str(d).encode('utf8')
print(data)
nodeJs
var utf = require('utf8');
var python = require('python-shell');
var pyt = path.normalize('path/to/my/python.exe'),
scrp = path.normalize('path/to/my/scriptsFolder/'),
var options = {
mode: 'text',
pythonPath: pyt,
pythonOptions: ['-u'],
scriptPath: scrp,
encoding: 'utf8',
args: [message]
};
python.run('test.py', options, function (err, results) {
//here I need to decode 'results'
var originalString = utf.encode(results.toString());// that code is not working for me
});
I have used several libs like utf8 to decode but didn't help.
Can someone please give some idea how to make it work.
EDIT
I have to edit with some more info.
I have tried #smarx approach but did not work.
I have two cases:
1. if I send data as string from python here is what I get in nodeJs b'\xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x8b\xad\xeb\x8b\x88\xea\xb9\x8c? \xec\x9d\xb4\xed\x9a\xa8\xec\xa2\x85 \xea\xb3\xa0\xea\xb0\x9d\xeb\x8b\x98! \xeb\x8f\x99\xec\x96\x91\xeb\xa7\xa4\xec\xa7\x81\xec\x9e\x85\xeb\x8b\x88\xeb\x8b\xa4
2. if I encode data and send. I get �ȳ��Ͻʴϱ�? ��ȿ�� ������! �
I had the totally same issue on my project and now I finally found the answer.
I solved my problem by using these codes.
It works on windows (macOS and Linux, their default system encoding it 'utf8' so the issue doesn't happen).
I hope it might help you, too!
#in the python file that your javascript file will call by python-shell module put those code
import sys
sys.stdout.reconfigure(encoding='utf-8')
I found the hints from python-shell description.
feature >Simple and efficient data transfers through stdin and stdout streams
I'm still not sure what python.run does, since you won't share that code, but here's my version of the code, which is working fine:
test.py
print("안녕 세상")
app.js
const { exec } = require('child_process');
exec('python3 test.py', function (err, stdout, stderr) {
console.log(stdout);
});
// Output:
// 안녕 세상
I have the same issue when using python-shell.
Here is my solution:
The string after .encode('utf-8') is a binary string. So you need to print it on stdout directly.
in test.py, it print a utf-8 json which include some chinese char:
sys.stdout.buffer.write(json.dumps({"你好":"世界"}, ensure_ascii=False).encode('utf8'))
print() # print \n at ending to support python-shell in json mode
in main.js
let opt = {mode: 'json', pythonOptions: ['-u'], pythonPath: 'python', encoding: 'utf8'}
let pyshell = new PythonShell('lyric.py', opt);
pyshell.on('message', function (message) {
console.log(message); //*** The console msg may still wrong (still ���)
let json = JSON.stringify(message);
let fs = require('fs');
fs.writeFile('myjsonfile.json', json, 'utf8', function () {
}); //*** The output json file will be correct utf8 output
});
result:
This shows the msg is correctly receive in utf-8, because the json output is correct.
However console.log output apparently failed.
I don't know is there any way to fix console.log output. (Windows 10)
I had same trouble in using data(string) from python in node js.
I solved this problem in this way:
Try change default code page of Windows Console to UTF-8, if your code page of Windows Console is not UTF-8.
(In my case default code page was CP949.)
In my case:
I got message like ������ 2���� ��������.
I tried encoding on online (http://code.cside.com/3rdpage/us/url/converter.html)
then I found my strings encoded cp949 -> decoded utf-8.
I write this code for saving data in json in javascript and the error is:
Uncaught TypeError: file.open is not a function
var txtFile = "./tmp/test.txt";
var file = new File([""], txtFile);
var str = JSON.stringify("string");
file.open();
file.write(str);
file.close();
What is the problem?
Take a look at this, maybe you can use it or see how it's made:
https://github.com/eligrey/FileSaver.js
I have a large xmlText string I would like to either copy and paste somehow into a editor like notepadd++ that would be the easiest if possible
When I try to look at the variable in different browsers I cannot see the whole string. It truncates.
If that is not possible is there a way to write it to a local file.
I just tried to do this a few different ways and they have all failed.
Sounds like you are not allowed to write to local files ??
thanks for any help
var xmlText = new XMLSerializer().serializeToString(xmlDoc);
Try this
function convertXMLToString(node) {
if (node.xml) {
return node.xml;
} else if (XMLSerializer) {
var serializer = new XMLSerializer();
return serializer.serializeToString(node);
}
}
document.getElementById("#id").innerHTML = convertXMLToString(your_xml_there);
Note that it won't work on old browsers.