Shouldn't I simply get the array without being wrapped in a proxy?
There's a number of variables involved here. If all your components have the same API version, are in the same namespace, the Apex method is @AuraEnabled(cacheable=false), and you're using Lightning Web Security, then you might end up receiving a plain Object/Array; I forget if there are additional rules, these are just the ones I recall. If any of these are not true, you're most likely going to get a Proxy. If the variable is bound to any markup, it will also become a Proxy immediately.
However, I feel this may be an X-Y Problem. Simply put, the Proxy is transparent, meaning that the executing code can't tell the difference between an Array and a Proxy that wraps an Array. The Proxy is only there to enforce certain rules (e.g. @api variables are meant to be read-only). Whatever problem you're having, except possibly the annoyance of having to open [[Target]] to visually debug objects, the Proxy is not likely the problem.
Hello,
I am trying to parse a string without success using JSON.parse(). I was able to stringify the original object without much trouble.
Can someone help me please ?
Original object:
'929920425': {
data: '{"users":[{"name":"Adriel","id":"ckz1fv9ok00029cvuop85wiba","__typename":"User"}]}'
}
Here is the string:
'{"929920425":{"data":"{\"users\":[{\"name\":\"Adriel\",\"id\":\"ckz1fv9ok00029cvuop85wiba\",\"__typename\":\"User\"}]}"}}'
why JSON.stringify() and JSON.parse does not work?
JSON.parse throwing error parsing something that came from JSON.stringify
Javascript JSON.stringify function is not working - Stack Overflow
JSON parsing not working
Apparently, while putting code in at Update #1 above, another question I had placed in stackoverflow had a direct impact on the reason for this question.
why am I getting type errors when hovering over map after layers are set?
as such, I had a variable I called JSON elsewhere in the code. using JSON as a variable overridden all of the global JSON directives, which denied access to the parse function.
Thanks all guys.
I am able to parse it. Check here. You can refer to this for browser issue with JSON in IE.
var layer = '{"type":"polygon", "coordinates": "-34.32982832836202 149.88922119140625, -34.80027235055681 149.80682373046875, -34.74161249883173 150.30120849609375, -33.99802726234876 150.77362060546875, -33.97980872872456 150.27923583984375"}';
console.log(JSON.parse(layer));
const fse = require('fs-extra');
function generateJSONFilePath(media) {
const file_path = __dirname + `\\medias\\${media.constructor.name}\\${media.file_name}.json`;
console.log(`Generated JSON file path: ${file_path}`);
return file_path;
}
function getObjectFromJSONFile(file_path) {
return new Promise((resolve, reject) => {
fse.readFile(file_path, {encoding:'utf8'})
.then((data) => {
try {
return JSON.parse(data);
} catch (e) {
console.log(file_path);
throw e;
}
})
.then(resolve)
.catch(reject);
});
}
medias.map((media) => {
return fse.writeFile(generateJSONFilePath(media), JSON.stringify(media));
})
...
json_files.map((json_file) => {
// returns promise for reading JSON file to Promise.all
return getObjectFromJSONFile(path + '\\' + json_file);
})So the above code is cherry picked from a much larger NodeJS script, but essentially what's happening is that I'm creating some objects (of class Media), then storing them as JSON in JSON files. Then, later in my code, I check to see if there are any JSON files to read from (for medias that were created last time the script ran). As you can see, I'm using JSON.stringify() to write the objects to the JSON files, and I'm using JSON.parse() to parse the files after reading them. But for some reason, I keep getting errors being thrown by the JSON.parse() called, complaining about unexpected characters. Even if I run the script twice immediately, without modifying the JSON files in any way, I still get these errors...and if I look at the JSON itself, I can see the characters it's complaining about (sometimes it's a comma, sometimes a number, it's different for each file).
So if I'm quite literally using JSON.stringify() to write to a file, then reading it with JSON.parse() without modifying it at any point in between, why wouldn't the parse be working? Could it have something to do with the process of writing to the file? The fs-extra module I'm using is supposedly a drop in replacement for the fs module, and I've definitely done JSON read/writes before...
I think I'm mainly looking for ideas on what could be going wrong, as it's obviously pretty hard to tell from afar. I could show my whole script, and I'm glad to if someone actually wanted to take a look at it, but I'm certain nothing is happening to modify the files in any way...and the stringify never throws errors, only the parse...
Full code just in case but...it's a lot...
This is because JSON.stringify will return the return value of the toJSON function if it exists (Source).
For example:
JSON.stringify({a:1, toJSON: function(){ return "a"; }});
Will return:
"a"
This behaviour is described on MDN. The reason for this is so that you can customize the behaviour of the serialization. For example, say I only want to serialize the IDs of the Animal class in this example. I could do the following:
var Animal = function(id, name) {
this.AnimalID = id;
this.Name = name;
};
Animal.prototype.toJSON = function() {
return this.AnimalID;
};
var animals = [];
animals.push(new Animal(1, "Giraffe"));
animals.push(new Animal(2, "Kangaroo"));
JSON.stringify(animals); // Outputs [1,2]
If you do not want this behaviour then your current method works well; however, I would recommend not overwriting the behaviour of JSON.stringify, but rather name your method something else. An external library might be using the toJSON function in an object and it could lead to unexpected results.
The reason the native JSON.stringify doesn't work is because it can't stringify functions. By setting the only function, toJSON, to undefined, JSON.stringify returns the proper value. See this question for more details: JSON.stringify function
If you're trying to remove functions entirely, you can do this:
JSON.stringify(object, function(key, value) {
if (typeof value === "function") {
return undefined;
}
return value;
});
I'm trying to write a React program which involves parsing JSONs downloaded form the internet. However, it seems it is unable to parse any JSONs, since even if I manually type out a JSON for it to parse, it gives the following error:
Uncaught SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
Here is an example of a JSON it is meant to parse:
{ "success": true, "query": { "from": "USD", "to": "HKD", "amount": 1 }, "info": { "timestamp": 1660293003, "rate": 7.835385 }, "date": "2022-08-12", "result": 7.835385 }
I am using Visual Studio Code, if that makes any difference.