简体   繁体   中英

JSONPath expression to concate two or more JSONPath values

I have below JSON data. I have JPath to read FirstName and LastName but to read both values using a single JSONPath expression is not getting. Can someone help me here to read the values for both the elements?

I want to read like Name=Rob|Long using JSONPath expression. I tried a few combinations but not working

{
   "attributes":     {
          "type":  "Contacts",
           "url":  "/services/data/v36.0/sobjects/Contact/abc123"
    },
    "Id": "abc123",
    "Salutation":  "Mr.",
    "FirstName":  "Rob",
    "LastName":  "Long"
}

Thanks in advance

Try with JayWay JsonPath:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

This should do the trick: concat($.FirstName,"|",$.LastName)

However, it doesn't seem to work with other JsonPath implementations.

using Bazaarvoice/ jolt this can be easily achieved

--------JSON Input 

{
  "attributes": {
    "type": "Contacts",
    "url": "/services/data/v36.0/sobjects/Contact/abc123"
  },
  "Id": "abc123",
  "Salutation": "Mr.",
  "FirstName": "Rob",
  "LastName": "Long"
}

--------------spec

[
  {
    "operation": "modify-default-beta",
    "spec": {
      // String join the values in the array x, with a comma and a space
      "fullName": "=concat(@(1,FirstName),'|',@(1,LastName))"
    }
  }
]
--------------------output
{
  "attributes" : {
    "type" : "Contacts",
    "url" : "/services/data/v36.0/sobjects/Contact/abc123"
  },
  "Id" : "abc123",
  "Salutation" : "Mr.",
  "FirstName" : "Rob",
  "LastName" : "Long",
  "fullName" : "Rob|Long"
}

try this out at - https://jolt-demo.appspot.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM