1

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

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59

2 Answers2

0

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.

runningwild
  • 129
  • 12
  • It doesn't work with other implementations because it's not valid JSON Path. Jayway does a lot of extra stuff that's not considered standard. – gregsdennis Oct 21 '20 at 08:32
  • As I understand, what is considered the [standard for JsonPath](https://goessner.net/articles/JsonPath/) was published on a blog 13 years ago and doesn't seem to have changed since (unlike the [standard for XPath](https://www.w3.org/TR/2017/REC-xpath-31-20170321/) which is maintained by the W3C). So perhaps a JsonPath implementation containing non-standard features that people seem to need can be expected to establish a new consensus and become the new de facto standard. – runningwild Oct 22 '20 at 11:45
  • As far as I know, there was a standardization initiative based on Goessner's implementation some years ago but a standard never emerged, and newer implementations like JayWay added (and continue to add) new features. A standardization process would be great; actually, we should even think about using different tags for different JSONPath implementations that are not so similar anymore! – wp78de Oct 22 '20 at 18:00
  • 1
    We are working on an official RFC: https://github.com/jsonpath-standard/internet-draft – gregsdennis Oct 23 '20 at 20:40
0

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/

Ayush v
  • 351
  • 2
  • 9