JSON validation fails (RFC 4627) - json

I have an Api method which returns json data. When I try to validate the json data using the online json validator: http://pro.jsonlint.com/, with the compare option, giving url in one section and the output of the url in another section, the url section shows an error and the section with data copied and pasted is validated.
What could be the issue here?
UPDATE:
I copied the 2 outputs into notepad and did a file compare, there is a non-printable character at the begining of the output from url.
D:\>fc j1.js j2.js
Comparing files j1.js and J2.JS
***** j1.js
{
"responseStatus": null,
***** J2.JS
{
"responseStatus": null,
*****
The content-type of the api response is "application/json; charset=utf-8".

Without knowledge of your locale and character set, this is speculation; but the placement of the spurious text suggests that it may be a Unicode BOM. (Hmmm, six bytes? Two UTF-8 BOMs?)

Related

For Jmeter post request, how can I send multiple requests in body from CSV(already have JSON inside) file?

I have a csv file and it has hundred record of JSON. I want to send JSON in body of JMETER post request one by one from CSV.
I tried this and I am getting desired results, but it is adding " " to every variable or data.for example: while sending this as a body
[
{
"id":"1232435",
"ref":"88f000",
"data":"5a344f",
"number":"896751245"
}
]
jmeeter is processing this body as
"[
{
""id"":""1232435"",
""ref"":""88f000"",
""data"":""5a344f"",
""number"":""896751245""
}
]"
I want it to process same as in csv file.
enter image description here
I cannot reproduce your issue, double check your JSON file contents.
If I put the following line into CSV file:
[{"id":"1232435","ref":"88f000","data":"5a344f","number":"896751245"}]
it's being sent as it is without any extra quotation marks.
Also you have dataa in the CSV Data Set Config and data in the HTTP Request so it might be the case that you're not actually reading the file at all because given your current CSV Data Set Config setup you would get only first line of the file into the data variable to wit your request would look like [
So use Debug Sampler and View Results Tree listener combination to see whether the variable is correct and how it does look like. If there are still extra quotation marks they can be removed using __strReplace() function

JSON parsing -SyntaxError: Unexpected token in JSON at position 72

I have generate several one-liner JSON files(from excel) -which i attempted to read via the cordova mobile app. The file is being read but the problem is parsing it
example onliner Json File: Note the file is being read
inside onloadend:
{"name":"Acrobat Ant","id":"500","category":"Ants","features":{"format": {"cn":"hd_lst" ,"fm":"inline","edit": 0},"npma":"1.Ants//13.Wood-infesting Insects","compare2":"","length":"Adults 2.5-4mm // Queen 10mm","color":"Brown to black // "... (this is only partial
Error in Success callbackId: File899903995 : SyntaxError: Unexpected token   in JSON at position 72
cordova.js:310 SyntaxError: Unexpected token   in JSON at position 72
at JSON.parse (<anonymous>)
at FileReader.reader.onloadend (plugins.js:188)
at FileReader.readSuccessCallback (plugins/cordova-plugin-file/www/FileReader.js:164)
at Object.callbackFromNative (cordova.js:291)
at <anonymous>:1:9
This happens to ever single file i try. That position 72 (for this file) is at the end of the {"format"
word just before the colon :
As i said this happens to every other file except the position changes but all fail exactly right at the end of the format property.
I used https://jslint.com/ to check the syntax of the one line JSON and it says is good.
The curious thing is that if the one liner is changed to a layered format(via visual Code Shift-ALt-F)
as the sample breakdown shown below the JSON gets parse with no problem.
{
"name": "Acrobat Ant",
"id": "500",
"category": "Ants",
"features": {
"format": {
"cn": "hd_lst",
"fm": "inline",
"edit": 0
},
....etc
By the way The one line JSON files were generated from Excel and saved as UTF8 Without BOM. (It fails if i try to parse used it WITH BOM.)
So My dilemma is That I have over 200 files which I would like them to be read immediately once created without I having to do the manual extra step to turn them into a layered format JSON.
FYI I used exactly the suggestion on how to save excel data to UTF8 without BOM from
VBA : save a file with UTF-8 without BOM
please advise

Dump Chinese data into a json file

I am falling on a problem, while dumping a chinese data (non-latin language data) into a json file.
I am trying to store list into a json file with the following code;
with open("file_name.json","w",encoding="utf8") as file:
json.dump(edits,file)
It will dumped without any errors.
When i am viewing a file, it will look like this,
[{sentence: \u5979\u7d30\u5c0f\u8072\u5c0d\u6211\u8aaa\uff1a\u300c\u6211\u501f\u4f60\u4e00\u679d\u925b\u7b46\u3002\u300d}...]
And I also tried out, without encoding option.
with open("file_name.json","w") as file:
json.dump(edits,file)
My question is, why my json file look like this, and how to dump my json file with having chinese string instead of unicode string.
Any helps would be appreciated. Thanks : )
Check out the docs for json.dump.
Specifically, it has a switch ensure_ascii that if set to False should make the function not escape the characters.
If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is.

jmeter Invalid UTF-8 middle byte

I'm using jMeter to shoot json through post requests to my test server.
the following request always fail:
{
"location": {
"latitude": "37.390737",
"longitude": "-121.973864"
},
"category": "Café & Bakeries"
}
the error message in the response data is:
Invalid UTF-8 middle byte 0x20
at [Source: org.apache.catalina.connector.CoyoteInputStream#6073ddf0; line: 6, column: 20]
The request is not sent to the server at all.
other requests (e.g. replacing the value in category with other valid category like "Delis") works perfectly.
I guess it's an encoding issue related to "Café" but I don't know how to resolve it.
any idea?
Thanks!
in the HTTP request itself, it is possible to set "content encoding". I set there "utf-8" and it solved the problem
You'll probably need an HTTP header to post that JSON:
Content-Type: application/json; charset=utf-8
Without this, it's likely that the string isn't UTF-8–encoded. JSON should be in UTF-8, so the hex bytes for é should be 0xc3 0xa9.
Without that header, the byte sequence is probably 0xe9, which is in ISO-8859-1 encoding. That would explain the error, as UTF-8 sequences starting 0xe_ are 3-byte sequences, so it sees 0xe9 0x20 (where 0x20 is the space after the é) and complains about an "invalid middle byte".
Source: Posting a JSON request with JMeter
None of the above solutions worked for me in jmeter 5.2.1
What I tried?
Set the jmeter properties file: sampleresult.default.encoding=UTF-8 (Didn't work)
Set the header Content-Type=application/json;UTF-8 ( Didn't work)
Tried preProcessor sampler.getArguments().getArgument(0).setValue(new String(utf8Bytes)) ( Didn't work)
Fortunately I noticed a field "Content encoding" ( As mentioned by Ofir Kolar) which seem to worked finally

json character encoding problem

When I encode an array to JSON I get "u00e1" instead of á.
How could I solve the character encoding?
Thanks
Your input data is not Unicode. 0xE1 is legacy latin1/ISO-8859-*/Windows-1252 for á. \u00e1 is the JSON/JavaScript to encode that. JSON must use a Unicode encoding.
Solve it by either fixing your input or converting it using something like iconv.
The browser's default encoding is probably Unicode UTF-8. Try
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">.
One problem can be if you check the response only (the response is only a text but the JSON must be an object).
You have to parse the response text to be a javascript object first (JSON.parse in javascript) and after that the characters will become the same as on the server side.
Example:
On the server in the php code:
$myString = "árvízrtűrő tükörfúrógép";
echo json_encode($myString); //this sends the encoded string via a protocol that maybe can handle only ascii characters, so the result on the client side is:
On the client side
alert(response); //check the text sent by the php
output: "\u00e1rv\u00edzrt\u0171r\u0151 t\u00fck\u00f6rf\u00far\u00f3g\u00e9p"
Make a js object from the respopnse
parsedResponse = JSON.parse(response);
alert(parsedResponse);
output: "árvízrtűrő tükörfúrógép"