0

I am trying to convert string to JSONObject. This is my code:

JSONObject obj = new JSONObject(str);

Vehicle.feature file contains :

    Scenario: Create a vehicle with valid json request
    Given vehicle json for VehicleService
    """
        "{\"vin\" : \"VIN5\", \"brand\" : \"Toyota\", \"model\" : \"Innova\", \"year\" : \"2017\", \"color\" : \"Red\", \"modelCode\" : \"1234\", \"type\" : \"M\", \"countryCode\" : \"JP\", \"isConnected\" : \"true\", \"isActive\" : \"true\"}"
    """
    When performing POST on VehicleService url /add
    Then VehicleService should return status code 200

VehicleStepDefs contains:

@Given("^vehicle json for VehicleService$")
public void submitValidVehicleRequest(String vehicleJson) throws JSONException {
    JSONObject obj = new JSONObject(vehicleJson);
    request = given().and()
            .header("Content-Type", MediaType.APPLICATION_JSON)
            .accept(ContentType.JSON)
            .body(obj);
    request.then().log().all();
}

My error looks like this :

org.json.JSONException: Value {"vin" : "VIN5", "brand" : "Toyota", "model" : "Innova", "year" : "2017", "color" : "Red", "modelCode" : "1234", "type" : "M", "countryCode" : "JP", "isConnected" : "true", "isActive" : "true"} of type java.lang.String cannot be converted to JSONObject
        at org.json.JSON.typeMismatch(JSON.java:111)
        at org.json.JSONObject.<init>(JSONObject.java:159)
        at org.json.JSONObject.<init>(JSONObject.java:172)
        at com.examples.demo.VehicleStepDefs.submitValidVehicleRequest(VehicleStepDefs.java:43)
        at ?.Given vehicle json for VehicleService(Vehicle.feature:8)

What am I doing wrong?

3 Answers 3

1

Check your imports.

Here is the running code:

import org.json.JSONException;
import org.json.JSONObject;

public class TestJson {
    public static void main(String[] args) {
        try {
            JSONObject obj = new JSONObject("{\"vin\" : \"VIN5\", \"brand\" : \"Toyota\", \"model\" : \"Innova\", \"year\" : \"2017\", \"color\" : \"Red\", \"modelCode\" : \"1234\", \"type\" : \"M\", \"countryCode\" : \"JP\", \"isConnected\" : \"true\", \"isActive\" : \"true\"}");
            System.out.println(obj.get("model"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

Output: Innova

Sign up to request clarification or add additional context in comments.

Comments

0

I am using the latest org.json jar json-20190722.jar

And i am able to print the json string without any issues, you can refer my code:

public static void main(String[] args) {

        String str = "{\"vin\" : \"VIN5\", \"brand\" : \"Toyota\", \"model\" : \"Innova\", \"year\" : \"2017\", \"color\" : \"Red\", \"modelCode\" : \"1234\", \"type\" : \"M\", \"countryCode\" : \"JP\", \"isConnected\" : \"true\", \"isActive\" : \"true\"}";

        JSONObject obj = new JSONObject(str);
        System.out.println(obj.getString("brand"));
        // this also works when we are not sure of the return type of the resultant object
        System.out.println(obj.get("brand"));

    }

When i try changing the

System.out.println(obj.getString("brand"));

to

System.out.println(obj.getJSONArray("brand"));

I got this exception

Exception in thread "main" org.json.JSONException: JSONObject["brand"] is not a JSONArray. at org.json.JSONObject.getJSONArray(JSONObject.java:752) at com.christmas.Main.main(Main.java:14)

which is clearly mentioning that the result is not of JsonArray type.

It is better you post your logic and complete stacktrace .

1 Comment

I am writing cucumber tests. I have to pass json into the request body. I have updated the question with more details
0

It seems the response string was encoded with another type (such as UTF-8 with BOM). Try adding following code snippet then convert it again.

vehicleJson = vehicleJson.substring(vehicleJson.indexOf("{"), vehicleJson.lastIndexOf("}") + 1);
JSONObject obj = new JSONObject(vehicleJson);
...

1 Comment

Now I'm getting the exception : org.json.JSONException: Expected literal value at character 1 of {\"vin\" ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.