1

Im trying to validate the below json using Jsonpath. I am not able to validate any values after the "fieldMap". I get No JSON path error on everything that I tried. Please advise on how to validate the json string.

{
 "Identifier": "ABC#SOMEYNC001#112#2018071900:00",
  "someEventId": "Some001",
  "Error": null,
  "fieldMap": {
    "responseKey [someType=APPLICANT, somebaseid=982465, somechildid=Some001]": {
      "Gur.CalledFlag": "Y",
      "Some.SomeField01": "null",
      "Some.SomeField02": "null",
      "Some.SomeField03": "null",
      "Some.SomeField04": "null",
     },
    "responseKey [someType=APPLICANT, somebaseid=982455, somechildid=Some001]": {
      "Gur.CalledFlag": "Y",
      "Some.SomeField01": "null",
      "Some.SomeField02": "null",
      "Some.SomeField03": "null",
      "Some.SomeField04": "null",
     }
  },
  "someMap": {
    "982465": {
      "urlMap": "http://someurl.com"
    },
    "982455": {
      "urlMap": "http://someurl.com"
    }
  }
}

And this is how I try to validate, in which I am failing.

this.mockMvc
.perform(get("/v1.0/someapi/ABC/{SomeEventId}",
        "Some001",
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.Identifier").value(notNullValue()))
        .andExpect(jsonPath("$.someEventId").value("Some001")))
        //.andDo(MockMvcResultHandlers.print())
        .andExpect(jsonPath("Error").value(nullValue()));
        //.andExpect(jsonPath("$.fieldMap").value(nullValue()))
        //.andExpect(jsonPath("$..[Gur.CalledFlag]").value(nullValue()));

Im a novice in this, any help is greatly appreciated!

emjay
  • 33
  • 1
  • 3
  • This looks incorrect: `.andExpect(jsonPath("Error").value(nullValue())`, that should read: `.andExpect(jsonPath("$.Error").value(nullValue())`. Other things to consider: (1) uncomment `.andDo(MockMvcResultHandlers.print())` so that you can see exactly what's returned and (2) remove all `jsonPath` statements except the first one and once you have that working start adding the others, one at a time. – glytching Aug 21 '18 at 07:55
  • @glytching, thanks for your response. The code which is uncommented works fine. I have added the exact response that I get to the question. I am unable to validate "fieldMap" and its child elements. – emjay Aug 21 '18 at 08:01
  • Your attribute names (especially those which containe ".", "[", "]') aren't very friendly to jsonpath. You can address the child elements of `fieldMap` like so: `$.fieldMap.*.['Gur.CalledFlag']` but I think you'll strugle to address attributes with names such as `responseKey [someType=APPLICANT, somebaseid=982455, somechildid=Some001]`. – glytching Aug 21 '18 at 08:15
  • @glytching, that's exactly my problem here. responseKey [someType=APPLICANT, somebaseid=982455, somechildid=Some001]. I am unable to validate response key and it's properties or the child elements based on response key. – emjay Aug 21 '18 at 09:36
  • Looks like you have emdedded some attribute values in a json attribute name. It's going to be difficult (perhaps impossible) to evaluate those attribute names using jsonpath. – glytching Aug 21 '18 at 09:51
  • Hoping for a solution here.. fingers crossed! Thank you – emjay Aug 21 '18 at 09:53

0 Answers0