3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
amit
  • 181
  • 2
  • 4
  • 20
  • your question is very abstract, what parser you are using, this is client application, or a web application, from where `$.messages.` is coming, is it a kind of templating? – silentsudo Dec 09 '19 at 03:55
  • not sure if https://stackoverflow.com/questions/23897661/or-operator-in-jsonpath is relevant – Jeremy Kahan Dec 09 '19 at 04:06
  • What did you mean "each read operation returns sorted list"? – LHCHIN Dec 09 '19 at 07:13
  • @silentsudo I am using com.jayway.jsonpath.JsonPath parser. Its a client. Messages are coming from slack api response. – amit Dec 11 '19 at 04:03
  • @LHCHIN the response by the api is in sorted manner. Thus every time I parse response it gives me sorted list. – amit Dec 11 '19 at 04:04
  • OK, so the response has been sorted by itself and you cannot prevent it from sorting. You invoked the API only once and it returned the sorted result, then you performed `JsonPath.read(document, jsonPathExpression)` twice to the sorted result and combined them, right? – LHCHIN Dec 11 '19 at 11:43
  • 1
    What are your exact intentions of combining the found and sorted messages with their seemingly optional nested "previous" messages (or even "next", as I can see from the [docs](https://api.slack.com/methods/search.messages) - but they don't describe the data model behind though)? Does it really make sense to make one sorted list from them all? Sorted by which property? I would suggest to give a concrete example of expected vs. obtained results... – Petr Bodnár Dec 12 '19 at 20:47
  • @LHCHIN yes that's correct . – amit Dec 18 '19 at 00:50

1 Answers1

3

You can get both values using this expression:

$.messages.matches[*]..text

Anar Sultanov
  • 3,016
  • 2
  • 17
  • 27
  • When i refreshed to answer your answer popped :P – lscoughlin Dec 13 '19 at 10:36
  • Thanks @Anar. I did try this but i returns a huge list which is difficult to parse further. I need to filter using 2 criterias .text and previous.text. – amit Dec 17 '19 at 01:43
  • @amit I think this is the closest you can get using JSONPath, since it does not support union of separate paths. – Anar Sultanov Dec 17 '19 at 06:36
  • Thanks @Anar. Can you please suggest any other library which can help? – amit Dec 18 '19 at 00:46
  • 1
    @amit since you do not need to deserialize whole objects, but only get the attributes, you probably need a library that supports JSON tree representation, e.g. Jackson, GSON, etc. – Anar Sultanov Dec 18 '19 at 06:39
  • @AnarSultanov I will try this. – amit Dec 19 '19 at 03:21
  • @AnarSultanov I think this would work if there is single set of data. In my case json includes multiple messages. Its not only 1 message object. – amit Dec 19 '19 at 03:50
  • It works with any set of data. Please read the library documentation that you have chosen on how to iterate through an array. I do not think that comments are the right place to discuss this. – Anar Sultanov Dec 19 '19 at 05:25