Json path example

Similar to XPATH, JSONPath is a simple way to extract parts of a given document. The example below uses Github Issues JSON and due to the length of JSON it won't be included in the example.

Get all issues ids

@Test
public void all_issues_ids () {
    
    List<String> issueIds = JsonPath.read(githubIssues, "$.[*].id");

    logger.info(issueIds);
    
    assertEquals(8, issueIds.size());
}

First issue title

@Test
public void first_issue_title () {
    
    String firstIssuesTitle = JsonPath.read(githubIssues, "$.[0].title");
    
    logger.info(firstIssuesTitle);
    
    assertEquals("Use a generic origin (not the S3 origin) when creating CloudFront dist", firstIssuesTitle);
}

Issues authored by

@Test
public void issues_authored_by () {
    
    List<Object> issuesByAuthor = JsonPath.read(githubIssues, 
            "$.[*].user[?(@.login == 'laurilehmijoki')]");
    
    logger.info(issuesByAuthor);

    assertEquals(3, issuesByAuthor.size());
}

Issues labeled bug

@Test
public void issues_labeled_as_bug () {
    
    List<Object> bugs = JsonPath.read(githubIssues, "$..labels[?(@.name==bug)]"); 
    // OR filter(where("name").is("bug")));
    
    logger.info(bugs);

    assertEquals(1, bugs.size());
}

Open issues >= 30

@Test
public void issues_by_state_and_number () {
    
    Filter<?> filter = Filter.filter(
            Criteria.where("state")
                .is("open")
                .and("number")
                .gte(30));
    
    List<Object> issuesByLabelAndAuthor = JsonPath.read(githubIssues, "$.[?]", filter);
    
    logger.info(issuesByLabelAndAuthor);

    assertEquals(1, issuesByLabelAndAuthor.size());
}