2

I have a goal to verify that certain JSON that I've got from RabbitMQ corresponds to one of expected JSONs in an array in a single file.

In other words, I need to verify that this JSON:

{
  "networkCode":"network",
  "programId":"92000"
}

is present in this JSON array:

[
{
   "networkCode":"network",
   "programId":"92000"
},
{
   "networkCode":"network",
   "programId":"92666"
}
]

Thank you very much for help!

Some part of my code

//GET DESIRABLE JSON
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            JSONObject myJSON= new JSONObject(message);

//GET THE JSON ARRAYS FROM FILE
            JSONParser parser = new JSONParser();
            Object expectedJSONs= parser.parse(new FileReader("C:\\amqpclient\\src\\test\\java\\tradeDoubler\\ExpectedDTO.json"));
            JSONArray expectedArray = (JSONArray) expectedJSONs;

            JSONAssert.assertEquals(
                    myJSON, expectedArray , JSONCompareMode.LENIENT);

Compilation says that cannot resolve this

Exception in thread "main" java.lang.AssertionError: Expecting a JSON array, but passing in a JSON object

4
  • 1
    Iterate over the array and check if the current object (in the array) equals the one you get from RabbitMQ Commented Jun 7, 2019 at 12:53
  • @Lino If I knew how, I'd do that and won't ask Commented Jun 7, 2019 at 12:57
  • 1
    I can just guide you to the documentation as I wont write the code for you, especially the getJSONObject() and length() methods should help you to implement my suggestion from the other comment Commented Jun 7, 2019 at 13:01
  • What line does prompt the error? and what JSON library are you using? Commented Jun 7, 2019 at 13:16

3 Answers 3

1

Org.json library is quite easy to use.

Example code below:

import org.json.*;

JSONObject obj = new JSONObject(" yourJSONObjectHere ");

JSONArray arr = obj.getJSONArray("networkArray");
for (int i = 0; i < arr.length(); i++)
{
    String networkCode = arr.getJSONObject(i).getString("networkCode");
    ......
}

By iterating on your JSONArray, you can check if each object is equal to your search.

You may find more examples from: Parse JSON in Java

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

1 Comment

Thank you! This was the most helpfull. But I had to use .size() instead of length(). And I compared JSONObjects, so I had to get a single object from an array : JSONObject object = array.getJSONObject(n); and use this JSONAssert.assertEquals("TEST FAILED", message, object.toJSONString(), JSONCompareMode.LENIENT); Yes, I know that I could create a String and cast JsonArray item to String
1

May I suggest you to use the Gson Library? You can use something like this. But It will throw an exception if the json doesn't match/contains the fields.

Type listType = new TypeToken<ArrayList<YourJavaClassJsonModel>>() {
        }.getType();
        List<YourJavaClassJsonModel> resultList = gson.fromJson(JsonString, listType);

Hope it may help

Comments

0

You could use a JSON parser to convert the JSON to a Java object (Jackson and GSON are good options), and then check that object.

Comments

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.