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"
}
Related
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
I've got a JSON string persisted in a DB (in a column of type Text), which I would like for clients of the server to be able to retrieve through a REST API.
Problem is that once the JSON string is retrieved by the backend and stored in a String object, ready to be returned to the client as part of the response body, Java automatically escapes the quotes.
So a JSON string persisted as:
{"key1": "value1", "key2": "value2"}
Would be retrieved and stored in a string object as:
{\"key1\": \"value1\", \"key2\": \"value2\"}
Then this escaped string would be returned to the client. My worry is that in the format that it is it won't be usable by the client.
I've seen some people using regex to remove the backslash, but I'm not sure how feasible that would be as a solution.
Any suggestions?
You can use org.apache.commons.text.StringEscapeUtils class for doing this. Please refer below code
String s = "{\\\"key1\\\": \\\"value1\\\", \\\"key2\\\": \\\"value2\\\"}";
s = StringEscapeUtils.unescapeJava(s);
System.out.println(s);
gives
{"key1": "value1", "key2": "value2"}
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
So I have been using jayway JSONPath to query JSON Objects much like the following:
{
"messageHeader" : {
"sentBy" : "someOne",
"userName" : "John Doe"
},
"payload" : []
}
And this is working fine for the most part, except now I wish to select the root level objects using the path $..* and preform separate tasks depending on the type of object present in the message, using their key/names as an identifier.
However, using said path, or $.* , will always produces a JSONArray much like this:
[{sentBy:someOne,userName:John Doe},[]]
The JSON objects appear to be anonymous, they have no keys. Is there anyway I can access the key for these objects directly as a String? If the data is not present, then why does the path: $.messageHeader.sentBy , work?
From README of the JsonPath :
When evaluating a path you need to understand the concept of when a
path is definite. A path is indefinite if it contains:
.. - a deep scan operator
?() - an expression
[, (, )] - multiple array indexes
Indefinite paths always returns a list (as represented by current JsonProvider).
This should explain the above phenomenon.
I have a string response like below which is a invalid json as it contains "obj13=".I want to convert it to a JSONObject(JAVA) and use it.Is there any good way to convert it to JSONObject without using String split operation.
obj13={
players: [
{
name: "rocky",
place: "brazil",
age: "21",
},
{
name: "andy",
place: "New Zealand",
age: "23",
}
]
}
This is, of course, JavaScript, not JSON. If you can, I would go back to the service provider and ask for a JSON response.
If the format of the string is consistent, you could just use:
json=json.substring(json.indexof('=')+1);
and then parse the result. Note that most good parsers should have an option to allow the keywords without quotes and to allow the extraneous commas (mine does, but unfortunately for you it doesn't create JSONObject's but is of a lower level - it's designed to construct the data-structure of the caller's choice, which could be a JSONObject if that's what you wanted but you'd have to code it).
If the result may or may not have the assignment, you may want to get a bit fancier and ensure that the non-whitespace characters before the '=' are valid for a JS identifier and the first non-whitespace after it is '{'.