0

I am taking a Github json file and parsing it with Java's regular expression library JsonPath. I am having a problem parsing arrays that do not have labels.

I need to send a email every time a particular file is changed in our repository.

Here is the Git Json:

{
  "trigger": "push",
  "payload": {
    "type": "GitPush",
    "before": "xxxxxxxx",
    "after": "yyyyyyyy",
    "branch": "branch-name",
    "ref": "refs/heads/branch-name",
    "repository": {
      "id": 42,
      "name": "repo",
      "title": "repo",
      "type": "GitRepository"
    },
    "beanstalk_user": {
      "type": "Owner",
      "id": 42,
      "login": "username",
      "email": "user@example.org",
      "name": "Name Surname"
    },
    "commits": [
      {
        "type": "GitCommit",
        "id": "ffffffff",
        "message": "Important changes.",
        "branch": "branch-name",
        "author": {
          "name": "Name Surname",
          "email": "user@example.org"
        },
        "beanstalk_user": {
          "type": "Owner",
          "id": 42,
          "login": "username",
          "email": "user@example.org",
          "name": "Name Surname"
        },
        "changed_files": {
          "added": [
            "NEWFILE",

          ],
          "deleted": [
            "Gemfile",
             "NEWFILE"
          ],
          "modified": [
            "README.md",
            "NEWFILE"
          ],
          "copied": [
          ]
        },
        "changeset_url": "https://subdomain.github.com/repository-name/changesets/ffffffff",
        "committed_at": "2014/08/18 13:30:29 +0000",
        "parents": [
          "afafafaf"
        ]
      }
    ]
  }
}

This is the expression I am using: to get the commits

$..changed_files

This return the whole changed files part but I can not explicitly choose the name "NEWFILE"

I tried

$..changed_files.*[?(@.added == "NEWFILE")]

$..changed_files.*[?(@.*== "NEWFILE")]

It just returns a empty array.

I just want it to return Newfile and what type of change. Any Ideas?

Aaron
  • 24,009
  • 2
  • 33
  • 57
paulx9000
  • 11
  • 1
  • Why do so many people try to parse JSON with regular expressions? Use a JSON parser, it's so much easier! And supported in just about every language. – joanis Aug 23 '19 at 15:27
  • @joanis, OP is using a json parser but some times you need to use regex within the key or value to match. – Josh Beauregard Aug 23 '19 at 15:58
  • It's not one of those times though, so I've removed the regex tag – Aaron Aug 23 '19 at 16:10

1 Answers1

0

You can use the following JsonPath to retrieve the commits which list "NEWFILE" as an added file :

$.payload.commits[?(@.changed_files.added.indexOf("NEWFILE") != -1)]
Aaron
  • 24,009
  • 2
  • 33
  • 57