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

  • 1
    Iterate over the array and check if the current object (in the array) equals the one you get from RabbitMQ – Lino Jun 07 '19 at 12:53
  • @Lino If I knew how, I'd do that and won't ask – Dexter Denmark Jun 07 '19 at 12:57
  • 1
    I can just guide you to the [documentation](http://www.docjar.com/docs/api/org/codehaus/jettison/json/JSONArray.html) 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 – Lino Jun 07 '19 at 13:01
  • What line does prompt the error? and what JSON library are you using? – Bentaye Jun 07 '19 at 13:16

3 Answers3

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

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

BCh
  • 65
  • 11
  • 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 – Dexter Denmark Jun 10 '19 at 10:01
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.

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80