java - Get List by using Jsonpath -
i using jayway jsonpath 2.1.0 parse json string using jsonpath.
the json structure follows:
{ "status": "", "source": "", "processedrecords": 1, "totalrecords": 4, "description": "1 company(ies) added/updated, , 2 company(ies) failed", "payload": [{ "added/updated company(ies)": [ "com001file" ] }, { "failed company(ies)": [{ "id": "com003file", "index": "na", "errormsg": [ [ "invalid_location" ] ] }, { "id": "com002file", "index": "na", "errormsg": [ [ "inactive" ], [ "invalid_location", "inactive" ] ] }] }] }
now requirement list of ids errormsg
contains inactive
. achieve using filters shown below.
filter resposnefilter = filter.filter(criteria.where("errormsg").is( "inactive")); list<map<string, object>> respons = jsonpath.parse(response).read( "$.payload[*]", resposnefilter);
but output getting values in payload.
is possible expected results?
taking account json provided , reading through documentation of jayway jsonpath. found 2 solution resolve issue.
- solution 1 allows directly access errormsg field but, not applicbable when there more 1 companies have manually loop
- solution 2 allows constructions of custom predicate specific case , generic.
solution 1 filter have seem correct, shown below:
filter responsefilter = filter.filter(criteria.where("errormsg").is("inactive"));
looking json, payload can thought of starting point , structure seem fixed second value of payload
array failed companies , interested in hence, write json path follow:
map<string, object> inactivefailedcompany = parse(json).read("$.payload[1].['failed company(ies)'][1]", responsefilter);
if notice above, have specified payload[1]
, assume how json structured. have specified ['failed company(ies)'][1]
, access company has inactive
. can use solution loop loop through index of ['failed company(ies)'][1]
, print id follow:
system.out.println("company object > " + inactivefailedcompany.tostring()); //output: output > {id=com002file, index=na, errormsg=[["inactive"],["invalid_location","inactive"]]} system.out.println("company id > " + inactivefailedcompany.get("id")); //output: com002file
looking @ above solution, felt bad because not enough, read more in documentation , came across roll own predicate hence solution 2.
solution 2
as explained in documentation, tailored predicate's apply
method meet requirements , solution follow:
predicate inactivecompaniespredicate = new predicate() { @override public boolean apply(predicatecontext ctx) { if(ctx.item(map.class).containskey("errormsg") && ((jsonarray)((jsonarray) ctx.item(map.class).get("errormsg")).get(0)).get(0).equals("inactive")) { return true; } else { return false; } } };
now use above predicate follow:
list<map<string, object>> failedcompanies = parse(json).read("$.payload[1].['failed company(ies)'][?]", list.class, inactivecompaniespredicate);
and loop through list of companies , print id of failed companies follow:
for(map<string, object> object: failedcompanies) { system.out.println(object.get("id")); //output: com002file }
i reflect on scary if condition inside predicate follow:
left part of if condition - xx
this part filters contain errormsg field.
if(ctx.item(map.class).containskey("errormsg") && yy)
right part of if condition - yy
this part makes sure errormsg
inactive
if(xx & ((jsonarray)((jsonarray) ctx.item(map.class).get("errormsg")).get(0)).get(0).equals("inactive"))
here sample code used test (i imported jsonpath 2.1 , dependencies external jars eclipse)
import java.io.ioexception; import java.nio.charset.charset; import java.nio.file.files; import java.nio.file.paths; import java.util.list; import java.util.map; import net.minidev.json.jsonarray; import com.jayway.jsonpath.predicate; import static com.jayway.jsonpath.jsonpath.parse; public class jaywayjson { public static void main(string[] args) throws ioexception { string json = readfile("c:\\test_stackoverflow\\jayway.json", charset.defaultcharset()); /* * //solution 1 * * filter responsefilter = * filter.filter(criteria.where("errormsg").is("inactive")); * * map<string, object> inactivefailedcompany = * parse(json).read("$.payload[1].['failed company(ies)'][1]", * responsefilter); * * system.out.println("company object > " + * inactivefailedcompany.tostring()); //output: output > {id=com002file, * index=na, errormsg=[["inactive"],["invalid_location","inactive"]]} * * system.out.println("company id > " + * inactivefailedcompany.get("id")); //output: com002file */ // solution 2 predicate inactivecompaniespredicate = new predicate() { @override public boolean apply(predicatecontext ctx) { if (ctx.item(map.class).containskey("errormsg") && ((jsonarray) ((jsonarray) ctx.item(map.class).get( "errormsg")).get(0)).get(0).equals("inactive")) { return true; } else { return false; } } }; list<map<string, object>> failedcompanies = parse(json).read( "$.payload[1].['failed company(ies)'][?]", list.class, inactivecompaniespredicate); (map<string, object> object : failedcompanies) { system.out.println(object.get("id")); } } public static string readfile(string path, charset encoding) throws ioexception { byte[] encoded = files.readallbytes(paths.get(path)); return new string(encoded, encoding); } }
Comments
Post a Comment