amit
amit

Reputation: 191

Combine jsonpath expression to read 2 nested properties in JSON string

I am using Slack API (https://slack.com/api/search.messages) to fetch posts in slack channel and parse the posts using Java code.

Following is the sample Slack response where slack posts are 'text' under 'matches' and matches - previous' :

    {
  "ok": true,
  "query": "\"@DTR:JQL\" in:#sydtest-reporting",
  "messages": {
    "total": 16,        
    "matches": [
      {
        "iid": "370f1cc2-aef9-4681-93f1-fa3787ce9d17",
        "team": "T9180DGJH",
        "channel": {
          "id": "C9UPLJ9D2",
          "is_channel": true,              
          "name": "sydtest-reporting",              
          "teams": [
            "T9180DGJH"
          ]
        },
        "type": "message",
        "user": "W9G7PAUCF",
        "username": "200848",
        "ts": "1568962541.016200",
        "text": "@DTR:JQL#SYDEPS AND labels = Nov_2019_CSS_Cycle1",
        "previous": {
          "type": "message",
          "user": "WGGUL08NA",
          "username": "208977",
          "ts": "1568962184.015700",
          "text": "@DTR:2019 November Release",
          "iid": "4bdb76cf-015b-4b43-9608-013b56e47820",
        },

Above response is sorted ("sort", "timestamp"). I am using read() from com.jayway.jsonpath.JsonPath library to parse above JSON and using following 2 jsonPathExpression:

$.messages.matches[*].text
$.messages.matches[*].previous.text

Currently I am firing two separate JsonPath.read(document, jsonPathExpression) calls (one call for each jsonPathExpression above) and then make a combined list as below :

@DTR:JQL#SYDEPS AND labels = Nov_2019_CSS_Cycle1
@DTR:2019 November Release
... many more

Problem with this approach is, though each read operation returns sorted list, when I combine both lists, sorting is meaningless.

What I am looking for is, single read operation for both jsonPathExpression. I can use some other JSON lib if needed.

Upvotes: 3

Views: 2308

Answers (1)

Anar Sultanov
Anar Sultanov

Reputation: 3406

You can get both values using this expression:

$.messages.matches[*]..text

Upvotes: 3

Related Questions