Search code examples
javajsonjsonpathjson-path-expression

How to get the value of "name" field from the json using jsonpath expression


I want to get the value against "name" field from the below json. I tried using the tools http://jsonpathfinder.com/ and http://jsonpath.com/? I am using http://jsonpath.herokuapp.com/ to verify if the expression path is correct, but it always returns me as an incorrect expression.

{
  "data" : {
    "PensionRetriever(Customer=ABC, typeInfo=valid)" : {
      "name" : "lifeInsurance",
      "created_at" : 1552297775384,
      "attributes" : [ {
        "id" : "4044da39-c23b-4588-b6c4-975ce02e7cb2",
        "name" : "lifeInsurance",
        "created_at" : 1552297775384
   }]
    }
  }
}

I tried with $.data["PensionRetriever(Customer=ABC, typeInfo=valid)"].name , but this seems incorrect. Can you please tell me now to get the value of "name" , i.e., lifeInsurance


Solution

  • Use single quoted rather than double quoted names in the JSONPath expression, i.e.

    $.data['PensionRetriever(Customer=ABC, typeInfo=valid)'].name
    

    Using the online evaluators on http://jsonpath.herokuapp.com/, the usually reliable Jayway fails, apparently unable to digest the name 'PensionRetriever(Customer=ABC, typeInfo=valid)'. That's a Jayway bug. But Goessner succeeds, returning the expected

    [    "lifeInsurance" ]