I have JSON as string
"{nameBitsCount=131}"
I need, using Jackson: 1) Parse this JSON correctly. 2) Put result into Map<String, Long>
But I Getting exception.
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('n' (code 110)): was expecting double-quote to start field name
at [Source: (String)"{nameBitsCount=131}";
PS: I think I need to change style of JSON to this
"{\"nameBitsCount\":\"131\"}";
The JSON you send isn't valid
{nameBitsCount=131}
need to convert format :
{"nameBitsCount":131}
add {"} around the Key and change {=} to {:} the JSON format valid for Json Object :
{
"Key" : "Value"
// "VALUE" if use for String and char put {"} around the value VALUE ex: 0.0 , 1 , -50 , FALSE
}
Look this website :
JSON Website
JSON Syntax
Related
when parsing a string into a JsonTree, is there a way to get the offset and length of the parsed JsonNodes ?
eg.
final String json = "{"objects" : ["One", "Two", "Three"]}";
is there a way to know the TextNode representing "Two" is starting at position 23 ?
FYI, fixed by using the jackson stream api to parse the json message
There is a JAVA REST API (Put request) that I want to hit with values from a CSV file.
The CSV file is:
The JMETER CSV configuration is:
This is how I have set up the JMETER Configuration to hit the API:
The deserialisation on the Java side is not happening correctly. From POSTMAN, the following works:
{
"productId": "ABC",
"score": 4.42489
}
Why is the Jmeter POST configuration not working correctly?
Error: Received Unknown exception com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of double from String value '$score': not a valid Double value
Update: On doing this in Jmeter configuration:
{
"productId" : "${product}",
"score": "${score}"
}
I got the following error:
Received Unknown exception com.fasterxml.jackson.core.JsonParseException: Unexpected character ('T' (code 84)): was expecting comma to separate OBJECT entries at [Source: java.io.PushbackInputStream#4063ce7d; line: 2, column: 18] Similarly for M, M and R. So 4 errors in total.
Update 2:[Solved]The following CSV Data Set Configuration worked without any change in the actual CSV file!!
A Big Thank you to #user7294900 for the help!!
i do wonder if you might also have a problem with product_id being the parameter name versus productID that works directly in postman.
You forgot curly braces (this is not velocity/postman)
You need to send HTTP Request Body Data:
{
"productId": "${product}",
"score": "${score}"
}
Check the maunual for more details:
Variables are referenced as follows:
${VARIABLE}
Also your quotes in CSV file is redundant, either change Allow quoted data to true or better yet remove all quotes " from CSV file.
You don't need quotation marks around "${product}" and ${score} as:
Your CSV file already has quotations around ProductId I don't think you need duplicate ones
Your application expects a Double and you are basically sending a String
So change your payload to look like:
{
"productId" : ${product},
"score": ${score}
}
More information:
JSON Data Types
REST API Testing - How to Do it Right
Actually I am receiving a JSONArray and I loop over every JSONObject. However, some json fields contain special characters. Forxample: ä, ö, Ä and etc. A sample jsonobject is shown below:
{
"field1": null,
"field2": "Äpple",
"field3": 1,
"field4": null
}
field2 is being displayed as Ã\u201epple.
Is this some encoding issue? How can I fix this issue?
I am using the following piece of code to read the JSON objects in the array.
Object dataObject = res.getJSONObject(i).get(column);
tempObject.put(column, dataObject);
Now the tempObject contains the column 'field2' with the distorted value.
Have you tried to set response encoding to utf-8?
res.setContentType("application/json;charset=UTF-8");
If sample is given as array then to read that array , you can convert JSON array into JsonObject and then fetch field1,field2 values as string.
e.g
jarr.get(i).getAsJsonObject().get("field1").getAsString();
I am trying to use Rest Assured in the Serenity framework to validate an endpoint response. I send an xml body to the endpoint and expect a JSON response back like so:
{"Entry ID" : "654123"}
I want to send the XML and verify in the JSON response that the value of the key "Entry ID" is not empty or null. The problem is, the key has a space in it, and I believe it is causing an error. Here is what I have so far:
SerenityRest.given().contentType(ContentType.XML)
.body(xmlBody)
.when().accept(ContentType.JSON).post(endpoint)
.then().body("Entry ID", not(isEmptyOrNullString()))
.and().statusCode(200);
This produces the error:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unable to resolve class Entry
# line 1, column 33.
Entry ID
^
1 error
I have tried wrapping the "Entry ID" term in different ways to no avail:
.body("'Entry ID'", not(isEmptyOrNullString()))
.body("''Entry ID''", not(isEmptyOrNullString()))
.body("\"Entry ID\"", not(isEmptyOrNullString()))
.body("['Entry ID']", not(isEmptyOrNullString()))
.body("$.['Entry ID']", not(isEmptyOrNullString()))
Is it possible to get the value of a key that contains a space in Rest Assured?
You just need to escape the key with single quotes:
then().body("'Entry ID'", not(isEmptyOrNullString()))
Here's an example (tested in version 3.0.6):
// Given
String json = "{\"Entry ID\" : \"654123\"}";
// When
JsonPath jsonPath = JsonPath.from(json);
// Then
assertThat(jsonPath.getString("'Entry ID'"), not(isEmptyOrNullString()));
I'm calling a web service that returns JSON. Within that JSON I have a property that holds a URL. But the colon (:) within that URL is making Gson throw a gson.stream.MalformedJsonException error. I know these keys and values should be wrapped
JSON returned by web service:
{
ID=15;
Code=ZPFgNr;
UserName=https://www.google.com/accounts/o8/id?id=xxxxxx; //<--problem
FirstName=Joe
}
My Java:
resultData=((SoapObject) result).getProperty(0).toString();
User response = gson.fromJson(resultData, User.class);
I know these keys and values should be wrapped in double quotes. But they are not, and that seems to be the problem.
So my question is:
Should I be encoding this JSON before deserializing it somehow? If so, how?
or
Should I do a find and replace on https: and escape the colon, If so, how would I escape the colon?
JSON uses commas to separate attributes, colon to separate the attribute name from the attribute value, and double quotes around the names and the values. This is not valid JSON.
Here's valid JSON:
{
"ID" : "15",
"Code" : "ZPFgNr",
"UserName" : "https://www.google.com/accounts/o8/id?id=xxxxxx",
"FirstName" : "Joe"
}