Skip to content

Instantly share code, notes, and snippets.

@dannyduc
Last active December 23, 2015 14:09
Show Gist options
  • Save dannyduc/6647121 to your computer and use it in GitHub Desktop.
Save dannyduc/6647121 to your computer and use it in GitHub Desktop.
Sample JsonPath code
import com.jayway.jsonpath.JsonPath;
public class Main {
public static final String JSON = "{\"links\":[{\"href\":\"http://localhost:8123/datastream/api/v1/activations/07cba26b-3916-43ba-9ac5-b07b7504d23a-126\",\"rel\":\"self\",\"method\":\"GET\"},{\"href\":\"http://localhost:8123/datastream/api/v1/activations/07cba26b-3916-43ba-9ac5-b07b7504d23a-126\",\"rel\":\"update\",\"method\":\"POST\"},{\"href\":\"http://localhost:8123/datastream/api/v1/activations/07cba26b-3916-43ba-9ac5-b07b7504d23a-126/validate\",\"rel\":\"validate\",\"method\":\"GET\"},{\"href\":\"http://localhost:8123/datastream/api/v1/activations/07cba26b-3916-43ba-9ac5-b07b7504d23a-126/execute\",\"rel\":\"execute\",\"method\":\"POST\"}],\"activation\":{\"id\":\"07cba26b-3916-43ba-9ac5-b07b7504d23a-126\",\"userName\":null,\"state\":\"VALIDATED\",\"creationDate\":null,\"lastModified\":{\"year\":2013,\"era\":1,\"dayOfYear\":263,\"dayOfWeek\":5,\"dayOfMonth\":20,\"centuryOfEra\":20,\"yearOfEra\":2013,\"yearOfCentury\":13,\"weekyear\":2013,\"monthOfYear\":9,\"weekOfWeekyear\":38,\"millisOfSecond\":629,\"millisOfDay\":75511629,\"secondOfMinute\":31,\"secondOfDay\":75511,\"minuteOfHour\":58,\"minuteOfDay\":1258,\"hourOfDay\":20,\"zone\":{\"fixed\":false,\"uncachedZone\":{\"cachable\":true,\"fixed\":false,\"id\":\"America/Los_Angeles\"},\"id\":\"America/Los_Angeles\"},\"millis\":1379735911629,\"chronology\":{\"zone\":{\"fixed\":false,\"uncachedZone\":{\"cachable\":true,\"fixed\":false,\"id\":\"America/Los_Angeles\"},\"id\":\"America/Los_Angeles\"}},\"afterNow\":false,\"beforeNow\":true,\"equalNow\":false},\"selectedSamples\":[],\"activationCodes\":[{\"sampleId\":\"aa65372f-67ac-4f7c-bccd-0fb1bcaecb6c\",\"activationCode\":\"LabTest001\"}]},\"selectedSamples\":[],\"activationCodes\":[{\"sampleId\":\"aa65372f-67ac-4f7c-bccd-0fb1bcaecb6c\",\"activationCode\":\"LabTest001\"}]}";
public static void main(String[] args) {
JsonPath jsonPath = JsonPath.compile("$.activation.activationCodes[0].sampleId");
Object result = jsonPath.read(JSON);
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment