JSON parsing: Unexpected Token error - json

Im trying to parse a string to JSON and I'm getting an unexpected token error.
I am checking validity using http://json.parser.online.fr/ which comes up with no parse errors, but still says the eval fails due to an unexpected token. If you paste the JSON from below in to that website you can see that it finds an error, but doesn't specify what token is causing it.
Heres what I'm trying to parse.
{
"Polish": {
"Rent": [
{
"english": "a",
"audioUrl": "b",
"alternate": "c"
},
{
"english": "d",
"audioUrl": "e",
"alternate": "f"
}
]
}
}
Am I missing something obvious?
EDIT
There is an unprintable character inbetween the : and [ after the "Rent" key.
I am doing some replace() calls on the string prior to the parse attempt which are likely creating the problem.
prior to the parse that particular line is
"Rent":"[
I want to remove the doublequote between the : and [ sybmols.
So I am using:
var reg = new RegExp('":"', 'g');
var newStr = originalStr.replace(reg, '":');
I don't know why the above is causing the unprintable character though.
EDIT2
I did a quick check removing the Above replace() call pasted it into the validator, manually removed the doublequotes I was using replace() on, and the unreadable characters are still there. So the error is present in the original string. So more code :|
The string is being returned from an ajax call to a php script residing on a server. The PHP script is reading a directory on the server and populating nested associative array to produce the string which is sent back to the JS side, which edits and parses it (shown above).
Within the directories are JSON files, which I'm inserting the contents of into this nested array structure to complete the JSON hierarchy.
The unreadable characters were
ef bb bf
Which I googled and found to be the Byte Order Mark of the string representing the file contents.
So heres the PHP Code which reads the directories and JSON files creating a nested array structure to be JSON_encode()d and sent back to the JS
if ($langHandle = opendir($langDir)) {
while (false !== ($langEntry = readdir($langHandle))) {
$currentLangDir = $langDir . "/" . $langEntry;
if (is_dir($currentLangDir) && $langEntry != '.' && $langEntry != '..') {
$currentLang = array();
if ($currentLangHandle = opendir($currentLangDir)) {
while (false !== ($catEntry = readdir($currentLangHandle))) {
$currentCatFile = $currentLangDir . "/" . $catEntry;
if(is_file($currentCatFile) && $catEntry != '.' && $catEntry != '..') {
$currentCat = file_get_contents($currentCatFile);
$currentLang[removeFileExtension($catEntry)] = $currentCat;
}
}
}
$langArray[$langEntry] = $currentLang;
}
}
What can I do to fix these unwanted characters, a quick search on removing the BOM chars suggests it is a bad thing to do.

You probably have a non printable character that is not showing up in what you pasted in your question. I copied and pasted your text into the online parser at the link you provided and it parses cleanly.
Try copying and pasting your original text into this online hex dump website, and compare to what you get when you copy and paste from your SO question above... if they differ then you'll have a clue as to where the bogus character is.
Here's a screenshot of the output I got, which parses cleanly.

Bro, I was having a similar problem, check your file encoding (UTF-8) and (UTF-8 WITHOUT BOM) can make a difference.

Related

Roku ParseJSON gives Unknow Identifier error when loading json via AJAX

I'm trying to write a simple Roku application.
When I load the JSON file via roURLTransfer ParseJSON function gives me BRIGHTSCRIPT: ERROR: ParseJSON: Unknown identifier.
If I load the JSON file via ReadAsciiFile("pkg:/feed/feed.json") it works.
The JSON file is the same and I'm pretty sure that my JSON is correct.
url = "http://192.168.1.36/misc/roku/ifilm/feed.json"
result = ""
timeout = 10000
ut = CreateObject("roURLTransfer")
ut.SetPort(CreateObject("roMessagePort"))
ut.SetURL(url)
if ut.AsyncGetToString()
event = wait(timeout, ut.GetPort())
if type(event) = "roUrlEvent"
result = event.GetString()
elseif event = invalid
ut.AsyncCancel()
else
print "roUrlTransfer::AsyncGetToString(): unknown event"
end if
end if
' `print result` shows the correct lintable JSON
' print result
' Next line gives me: BRIGHTSCRIPT: ERROR: ParseJSON: Unknown identifier
json = ParseJSON(result)
But putting the JSON file inside the app works:
feed = ReadAsciiFile("pkg:/feed/feed.json")
sleep(2000)
json = ParseJson(feed)
I need to load the data from the Internet and using the embedded version doesn't help me. Does anyone know what should I do to make it work?
The "Unknown identifier" error is usually because there's a character in the json string that ParseJson() does not support. The reason why ReadAsciiFile() works is likely because the function "cleans up" the json string by applying UTF-8 encoding.
A common character that's present at the beginning of some JSON responses that causes this issue is the unicode character Byte Order Mark (BOM)
If you google "byte order mark json" you'll see lots of cases where this affects other platforms as well.
You can just do a simple find and replace to get rid of that character before attempting to parse the string.
bomChar = Chr(65279)
if result.left(len(bomChar)) = bomChar ' Check if the string has the BOM char prefix
result = result.replace(bomChar, "")
end if
If that doesn't work, then your response may have some other conflicting character, in that case I would advise using ifUrlTransfer::AsyncGetToFile() instead of AsyncGetToString() and then use ReadAsciiFile() which should guarantee a properly formatted json string every time (as long as your json is valid).

Ambiguous format output in nodejs

I am having output in following format as
"[{"a":"a1"},{"a":"a2"}]"
I want to actually extract it in array of json:
[
{
"a":"a1"
},
{
"a":"a2"
}
]
How to convert it?
You have tagged this with Node-RED - so my answer assumes that is the environment you are working in.
If you are passing a message to the Debug node and that is what you see in the Debug sidebar, that indicates your msg.payload is a String with the contents of [{"a":"a1"},{"a":"a2"}] - the Debug sidebar doesn't escape quotes when displaying strings like that.
So you likely already have exactly what you want - it just depends what you want to do with it next.
If you want to access the contents you need to parse it to a JavaScript Object. You can do this by passing your message through a JSON node.
Assuming your input contains the double quotes in the beginning and end, it is not possible to directly JSON.parse() the string.
In your case, you need to remove the first and last character (the double quotes) from your string before parsing it.
const unformattedString = '"[{"a":"a1"},{"a":"a2"}]"'
const formattedString = unformattedString.substr(1, unformattedString.length - 2)
const json = JSON.parse(formattedString)
The variable json now contains your JSON object.
I would suggest a different method which will get your work done without using any third party library.
var a = '[{"a":"a1"},{"a":"a2"}]';
var b = JSON.parse(a);
console.log(b); // b will return [{"a":"a1"},{"a":"a2"}]
Another way which is eval function which is generally not recommended
var d = eval(a);
If you want to use JQuery instead use :
var c = $.parseJSON(a);

JSON.parse and JSON.stringify are not idempotent and that is bad

This question is multipart-
(1a) JSON is fundamental to JavaScript, so why is there no JSON type? A JSON type would be a string that is formatted as JSON. It would be marked as parsed/stringified until the data was altered. As soon as the data was altered it would not be marked as JSON and would need to be re-parsed/re-stringified.
(1b) In some software systems, isn't it possible to (accidentally) attempt to send a plain JS object over the network instead of a serialized JS object? Why not make an attempt to avoid that?
(1c) Why can't we call JSON.parse on a straight up JavaScript object without stringifying it first?
var json = { //JS object in properJSON format
"baz":{
"1":1,
"2":true,
"3":{}
}
};
var json0 = JSON.parse(json); //will throw a parse error...bad...it should not throw an error if json var is actually proper JSON.
So we have no choice but to do this:
var json0= JSON.parse(JSON.stringify(json));
However, there are some inconsistencies, for example:
JSON.parse(true); //works
JSON.parse(null); //works
JSON.parse({}); //throws error
(2) If we keep calling JSON.parse on the same object, eventually it will throw an error. For example:
var json = { //same object as above
"baz":{
"1":1,
"2":true,
"3":{}
}
};
var json1 = JSON.parse(JSON.stringify(json));
var json2 = JSON.parse(json1); //throws an error...why
(3) Why does JSON.stringify infinitely add more and more slashes to the input? It is not only hard to read the result for debugging, but it actually puts you in dangerous state because one JSON.parse call won't give you back a plain JS object, you have to call JSON.parse several times to get back the plain JS object. This is bad and means it is quite dangerous to call JSON.stringify more than once on a given JS object.
var json = {
"baz":{
"1":1,
"2":true,
"3":{}
}
};
var json2 = JSON.stringify(json);
console.log(json2);
var json3 = JSON.stringify(json2);
console.log(json3);
var json4 = JSON.stringify(json3);
console.log(json4);
var json5 = JSON.stringify(json4);
console.log(json5);
(4) What is the name for a function that we should be able to call over and over without changing the result (IMO how JSON.parse and JSON.stringify should behave)? The best term for this seems to be "idempotent" as you can see in the comments.
(5) Considering JSON is a serialization format that can be used for networked objects, it seems totally insane that you can't call JSON.parse or JSON.stringify twice or even once in some cases without incurring some problems. Why is this the case?
If you are someone who is inventing the next serialization format for Java, JavaScript or whatever language, please consider this problem.
IMO there should be two states for a given object. A serialized state and a deserialized state. In software languages with stronger type systems, this isn't usually a problem. But with JSON in JavaScript, if call JSON.parse twice on the same object, we run into fatal exceptions. Likewise, if we call JSON.stringify twice on the same object, we can get into an unrecoverable state. Like I said there should be two states and two states only, plain JS object and serialized JS object.
1) JSON.parse expects a string, you are feeding it a Javascript object.
2) Similar issue to the first one. You feed a string to a function that needs an object.
3) Stringfy actually expects a string, but you are feeding it a String object. Therefore, it applies the same measures to escape the quotes and slashes as it would for the first string. So that the language can understand the quotes, other special characters inside the string.
4) You can write your own function for this.
5) Because you are trying to do a conversion that is illegal. This is related to the first and second question. As long as the correct object types are fed, you can call it as many times as you want. The only problem is the extra slashes but it is in fact the standard.
We'll start with this nightmare of your creation: string input and integer output.
IJSON.parse(IJSON.stringify("5")); //=> 5
The built-in JSON functions would not fail us this way: string input and string output.
JSON.parse(JSON.stringify("5")); //=> "5"
JSON must preserve your original data types
Think of JSON.stringify as a function that wraps your data up in a box, and JSON.parse as the function that takes it out of a box.
Consider the following:
var a = JSON.stringify;
var b = JSON.parse;
var data = "whatever";
b(a(data)) === data; // true
b(b(a(a(data)))) === data; // true
b(b(b(a(a(a(data)))))) === data; // true
That is, if we put the data in 3 boxes, we have to take it out of 3 boxes. Right?
If I put my data in 2 boxes and take it out of 1, I'm not holding my data yet, I'm holding a box that contains my data. Right?
b(a(a(data))) === data; // false
Seems sane to me...
JSON.parse unboxes your data. If it is not boxed, it cannot unbox it. JSON.parse expects a string input and you're giving it a JavaScript object literal
The first valid call to JSON.parse would return an object. Calling JSON.parse again on this object output would result in the same failure as #1
repeated calls to JSON.stringify will "box" our data multiple times. So of course you have to use repeated calls to JSON.parse then to get your data out of each "box"
Idempotence
No, this is perfectly sane. You can't triple-stamp a double-stamp.
You'd never make a mistake like this, would you?
var json = IJSON.stringify("hi");
IJSON.parse(json);
//=> "hi"
OK, that's idempotent, but what about
var json = IJSON.stringify("5");
IJSON.parse(json);
//=> 5
UH OH! We gave it a string each time, but the second example returns an integer. The input data type has been lost!
Would the JSON functions have failed us here?
var json = JSON.stringify("hi");
JSON.parse(json);
//=> "hi"
All good. And what about the "5" ?
var json = JSON.stringify("5");
JSON.parse(json));
//=> "5"
Yay, the types have been preseved! JSON works, IJSON does not.
Maybe a more real-life example:
OK, so you have a busy app with a lot of developers working on it. It makes
reckless assumptions about the types of your underlying data. Let's say it's a chat app that makes several transformations on messages as they move from point to point.
Along the way you'll have:
IJSON.stringify
data moves across a network
IJSON.parse
Another IJSON.parse because who cares? It's idempotent, right?
String.prototype.toUpperCase — because this is a formatting choice
Let's see the messages
bob: 'hi'
// 1) '"hi"', 2) <network>, 3) "hi", 4) "hi", 5) "HI"
Bob's message looks fine. Let's see Alice's.
alice: '5'
// 1) '5'
// 2) <network>
// 3) 5
// 4) 5
// 5) Uncaught TypeError: message.toUpperCase is not a function
Oh no! The server just crashed. You'll notice it's not even the repeated calling of IJSON.parse that failed here. It would've failed even if you called it once.
Seems like you were doomed from the start... Damned reckless devs and their careless data handling!
It would fail if Alice used any input that happened to also be valid JSON
alice: '{"lol":"pwnd"}'
// 1) '{"lol":"pwnd"}'
// 2) <network>
// 3) {lol:"pwnd"}
// 4) {lol:"pwnd"}
// 5) Uncaught TypeError: message.toUpperCase is not a function
OK, unfair example maybe, right? You're thinking, "I'm not that reckless, I
wouldn't call IJSON.stringify or IJSON.parse on user input like that!"
It doesn't matter. You've fundamentally broken JSON because the original
types can no longer be extracted.
If I box up a string using IJSON, and then unbox it, who knows what I will get back? Certainly not you, and certainly not the developer using your reckless function.
"Will I get a string type back?"
"Will I get an integer?"
"Maybe I'll get an object?"
"Maybe I will get cake. I hope it's cake"
It's impossible to tell!
You're in a whole new world of pain because you've been careless with your data types from the start. Your types are important so start handling them with care.
JSON.stringify expects an object type and JSON.parse expects a string type.
Now do you see the light?
I'll try to give you one reason why JSON.parse cannot be called multiple time on the same data without us having a problem.
you might not know it but a JSON document does not have to be an object.
this is a valid JSON document:
"some text"
lets store the representation of this document inside a javascript variable:
var JSONDocumentAsString = '"some text"';
and work on it:
var JSONdocument = JSON.parse(JSONDocumentAsString);
JSONdocument === 'some text';
this will cause an error because this string is not the representation of a JSON document
JSON.parse(JSONdocument);
// SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
in this case how could have JSON.parse guessed that JSONdocument (being a string) was a JSON document and that it should have returned it untouched ?

cordova readAsText returns json string that can't be parsed to JSON object

I read my json file using http and cordova file readAsText functions.
http request returns an object which is ok.
cordova file readAsText function return 'string' which contain extra "r\n\" symbols. This make it impossible to use JSON.parse(evt.target.result)
function readJson(absPath, success, failed){
window.resolveLocalFileSystemURL(absPath, function (entry) {
entry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (evt) {
success(evt.target.result);
};
reader.readAsText(file);
}, failed);
}, failed);
}
readJson(cordova.file.dataDirectory + 'my.json', function(res){
console.log(JSON.parse(res)); //here I've got an parsing error due to presence of r\n\ symbols
}, failed );
How to read JSON files using cordova?
UPDATE:
funny thing that the following works:
a = '{\r\n"a":"1",\r\n"b":"2"\r\n}';
b = JSON.parse(a);
so the problem not only with \r\n... there is something else that is added by cordova readAsText
UPDATE2
as a workaround I use now var object = eval("(" + res + ")")
Still search for a common way to load json objects...
No one has answered this and I just had to solve it for my project, so I will post my solution.
The readAsText method outputs a string, so you CAN actually run a replace on it, but what you need to do is use a RegExp to find the newline character. here's my solution:
var sanitizerRegex = new RegExp(String.fromCharCode(10), 'g');
var sanitizedData = JSON.parse(result.replace(sanitizerRegex, ''));
I've used the String method fromCharCode to get the specific newline character and the "g" flag to match all instances in the entire string. The problem with your string solution is that you can't do a string replace using the characters for backslash and "n" because the issue is the actual new line character, which is merely represented as "\n".
I do not know the reason JSON.parse can't handle the newline character, or why the file plugin introduces this problem, but this solution seems to work for me.
Also, NEVER use eval like this if you can avoid it, especially on input from a source like a JSON file. Even in a cordova app, using eval is potentially very unsafe.
I found out the solution after debug deeply. readAsText function returned text has one more letter at the first position of text.
Example:
{"name":"John"} => ?{"name":"John"} (?: API didn't return ?, just one string)
I confirmed this with length of result, so we need to use substr(1) before parse JSON.
fileContent = fileContent.substr(1);
var jData = jQuery.parseJSON(fileContent);

What is this file format called

I need to parse a file which is in the following format:
"General"
{
"Description" = "Some Text"
"Version" = "4"
"ProjType" = "1"
}
"Configurations"
{
"Mice"
{
"BuildOutputs" = "BuildProject"
"OutputFile" = "output.txt"
}
"Men"
{
"BuildOutputs" = "BuildProject"
"ChangedSinceLastBuilt" = "True"
}
}
Does anyone have any idea what file format this is? If it's well known then there could be already made libraries to help parse it. It appears to be similar to JSON but instead of colons it uses equals sign and instead of commas it forcefully uses a new line.
You could simply read in the entire file and then convert = to : and each line break with , - then you could parse it with an existing json library. Perhaps you would have to insert a ; at the end of the data.