1

I have following json data:-

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

Method to assert json:-

public void assertJsonvalue (String description, String jsonString,
            String path, Object expectedValue) {

        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> jsonMap = null;
        try {
            jsonMap = objectMapper.readValue(jsonString,
                    new TypeReference<Map<String, Object>>() {
                    });

        } catch (IOException e) {
            fail("Could not parse json from string:" + jsonString);
        }

        Object actualValue = null;
        try {
            actualValue = PropertyUtils.getProperty(jsonMap, path);
            System.out.println("actualValue" + actualValue);
        } catch (IllegalAccessException e) { 
          // error here
        }
        assertEquals(description, expectedValue, actualValue);

}

When I try to get json value from by using the following, Its works well.

    assertJsonValue("bicycle color", json, "store.bicycle.color", "red");

I want to get array value from json such as details of 1st book.

I have tried the following, that doesn't help me out.

json paths are as follows:-

  1. "store.book[0]"
  2. "store.book.[0]"
  3. "store.book.category"

How Can I do this ?

2
  • What library are you using Commented Apr 25, 2016 at 7:09
  • I am using following libraries such as com.fasterxml.jackson.databind.JsonNode and org.codehaus.jackson.JsonNode Commented Apr 25, 2016 at 7:44

1 Answer 1

3

According to this answer, you should be able to get the mentioned properties like this:

assertJsonValue("...", json, "store.(book)[0].category", "reference");

Edit:

Which version of jackson and beanutils are you using? I've modified your method a bit and made a simple test case, using beanutils 1.9.2 and jackson 2.6.5 tests seem to pass:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public class TestJson {

    private static final String JSON = "{\n" +
            "    \"store\": {\n" +
            "        \"book\": [\n" +
            "            {\n" +
            "                \"category\": \"reference\",\n" +
            "                \"author\": \"Nigel Rees\",\n" +
            "                \"title\": \"Sayings of the Century\",\n" +
            "                \"price\": 8.95\n" +
            "            }\n" +
            "        ],\n" +
            "        \"bicycle\": {\n" +
            "            \"color\": \"red\",\n" +
            "            \"price\": 19.95\n" +
            "        }\n" +
            "    },\n" +
            "    \"expensive\": 10\n" +
            "}";

    @Test
    public void testJson() {
        assertTrue(assertJsonValue(JSON, "store.(book)[0].category", "reference"));
        assertTrue(assertJsonValue(JSON, "store.(book)[0].author", "Nigel Rees"));
        assertTrue(assertJsonValue(JSON, "store.(book)[0].title", "Sayings of the Century"));
        assertTrue(assertJsonValue(JSON, "store.(book)[0].price", 8.95));
    }

    public boolean assertJsonValue(String jsonString,
                                   String path,
                                   Object expectedValue) {

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Object actual = PropertyUtils
                    .getProperty(objectMapper.readValue(jsonString, Object.class), path);

            if (actual.equals(expectedValue)) {
                return true;
            }

        } catch (IOException | ReflectiveOperationException e) {
            // handle error
        }
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Following doesn't work for me. Path ("store.(book)[0].category") returns "null".
Maybe you're using an older version of jackson or beanutils? I've updated my answer to show how I tested this out.
I have updated commons-beanutils version to 1.9.2. Now its works well. Thanks

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.