JSON.parse() fail to parse a String with a JSON Object inside - javascript

I have a JSON Object which contains the following structure:
{"msg":"text","id":integer}
The user enters the text and the object is generated in the server-side (Java) and then passed as a String to JSON.parse() on the client-side.
The problem is, when a user enters a JSON Object format as the text, the function fails with:
SyntaxError: Unexpected token E in JSON at position 1
The string that resulted this error is:
{"msg":"{ENABLE:0,MOVING:0,ERRMESSAGE:}","id":999}
Any ideas for a workaround? I don't understand why it's failing.

Related

why JSON.parse does not convert a string to json object?

I tried to convert a string to a javascript object using JSON.parse but it's not working.
I tried everything but did not get anything on the console.log and there is no error message.
JSON.parse(`{'exp': '1', 'input': '1d6404f66ed3d72e', 'iterate': 'no'}`);
Update
In the real code, I'm passing the value from an object
console.log(JSON.parse(future.onIOPub.data['text/plain']))
When you run this, you should see
Uncaught SyntaxError: Unexpected token ' in JSON at position 1
This error is because you are using single quotes. JSON only accepts double quotes, as described in the spec
https://www.json.org/json-en.html

Error with JSON.parse() of stringified array from sessionStorage

I need to store an array in sessionStorage and found this answer helpful: Storing Objects in HTML5 localStorage
Storing the array with sessionStorage.setItem('flavors', JSON.stringify(flavors)) and retrieving the string with JSON.parse(sessionStorage.getItem('flavors')) works the first time. Then, testing this plain-JavaScript PWA with the back- and forward- buttons, I get an error:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Depending on the elements of the array, the column number can vary.
I can avoid the error by using:
flavors = sessionStorage.getItem('flavors').split(",");
instead of JSON.parse(). Along with the error, I can console log the string which looks okay:
chocolate,vanilla,strawberry
What could be causing the error?
In order to parse JSON, the string must represent a valid JSON, in you case this JSON.parse('["chocolate","vanilla","strawberry"]') will work

SageMaker Javascript SDK Endpoint Invocation Error: "CustomerError: Unable to parse payload to numeric values"

I was invoking the SageMaker endpoint from my angular front-end when I came across this error on AWS CloudWatch regarding my model making inferences from the data (In the form of a comma-separated string with target values at the first index) I was sending : Unable to parse numeric values. The string I'm using to invoke the endpoint was : "1533071820,0.05619,0.05619,0.05611,0.05611,0.006076\n"
String request = "1533071820,0.05619,0.05619,0.05611,0.05611,0.006076\n"
ByteBufferbuf = ByteBuffer.wrap(request.getBytes());
invokeEndpointRequest.setBody(buf);
Use the SageMaker API
AmazonSageMakerRuntime amazonSageMaker = AmazonSageMakerRuntimeClientBuilder.defaultClient();
Invoke the model endpoint on SageMaker
InvokeEndpointResult invokeEndpointResult = amazonSageMaker.invokeEndpoint(invokeEndpointRequest);
The result I was expecting from the endpoint is a JSON object with the 'score' attribute in the format : {"predictions": [{"score": xxxxxxx}]}
I am getting a 'ModelError: Unable to evaluate payload' from the IDE logs and 'Unable to parse numeric values on CloudWatch'
So, upon debugging this issue, I found that because of the newline character at the end, the model was assuming there are 2 strings in my input but there's only one. Hence, the error. So I just changed the string to : "1533071820,0.05619,0.05619,0.05611,0.05611,0.006076". And if I'm parsing more than one payload(batch), it's: "1533071820,0.05619,0.05619,0.05611,0.05611,0.006076\n1533071820,0.05619,0.05619,0.05611,0.05611,0.006076" where my 2 inputs are separated by that newline character and more importantly, there's no newline character at the end

JSON Parse with square brackets inside data

I am trying to parse a JSON string which contains square brackets inside the string.
For example
"[[\"hello\", \"i\", \"like [cows]\"], [\"what\", \"about [you]?\"]]"
It shows an error while parsing:
Uncaught SyntaxError: Unexpected token [
How can I parse the string anyway while leaving the brackets as they are?
I found the problem. I needed to use addslashes() around the strings in the PHP code which serves the code. There was a part of a string containing [C:\].
I see in your later comments that you're creating this JSON string within PHP. You should encode the JSON data within PHP using json_encode, as shown here:
http://www.dyn-web.com/tutorials/php-js/json/array.php
It should then come out in a format that you can JSON.parse.

Trying to parse JSON string, unexpected number

I am trying to parse this JSON:
var json = '{"material":"Gummislang 3\/4\" 30 m (utanp\u00e5liggande sk\u00e5p)"}'
I run JSON.parse(json) but i get the error SyntaxError: Unexpected number when doing so. I have tried this in Google Chrome. I don't know what the problem is since I can take the JSON string and put it in any JSON validator and it claims that the JSON is valid. Shouldn't the browser be able to parse it?
You are inserting a JSON object representation into a JavaScript string without properly escaping the representation.
To avoid having to do this, remove the quotes you are adding around the representation, and skip the JSON.parse(json) – the default output from PHP's json_encode() is valid JavaScript when used in this context.
For security, you should specify the JSON_HEX_TAG option if possible. This will prevent cross-site scripting in cases where the JSON might end up inside a document parsed as XML. (And for XML documents, the JSON should be inside a CDATA section as well.)
You're validating the string literal, which is a valid JSON string containing invalid JSON. You need to validate the value of the string, which is not valid JSON.
If you paste the string value into a JSON validator, you'll see that the error comes from this part:
"material": "Gummislang 3/4"30m
The " needs to be escaped.

Categories

Resources