Home > OS >  How to get Json content in another Json content by Regex
How to get Json content in another Json content by Regex

Time:12-29

I have the Json content like this

{"expand": "names,schema","startAt": 0,"maxResults": 50,"total": 1,"issues": [{"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields","id": "641955","self": "https://rb-tracker.bosch.com/tracker19/rest/api/latest/issue/641955","key": "EDATOOL-1411","fields": {"created": "2022-12-06T11:14:36.000 0100","customfield_10000": "{summaryBean=com.atlassian.jira.plugin.devstatus.rest.SummaryBean@24a1688f[summary={pullrequest=com.atlassian.jira.plugin.devstatus.rest.SummaryItemBean@23f2f23c[overall=PullRequestOverallBean{stateCount=0, state='OPEN', details=PullRequestOverallDetails{openCount=0, mergedCount=0, declinedCount=0}},byInstanceType={}], build=com.atlassian.jira.plugin.devstatus.rest.SummaryItemBean@321de5ab[overall=com.atlassian.jira.plugin.devstatus.summary.beans.BuildOverallBean@6ca82c6[failedBuildCount=0,successfulBuildCount=0,unknownBuildCount=0,count=0,lastUpdated=<null>,lastUpdatedTimestamp=<null>],byInstanceType={}], review=com.atlassian.jira.plugin.devstatus.rest.SummaryItemBean@5fc3e1e6[overall=com.atlassian.jira.plugin.devstatus.summary.beans.ReviewsOverallBean@7a2d42e3[stateCount=0,state=<null>,dueDate=<null>,overDue=false,count=0,lastUpdated=<null>,lastUpdatedTimestamp=<null>],byInstanceType={}], deployment-environment=com.atlassian.jira.plugin.devstatus.rest.SummaryItemBean@2740d0b0[overall=com.atlassian.jira.plugin.devstatus.summary.beans.DeploymentOverallBean@75ceaa2c[topEnvironments=[],showProjects=false,successfulCount=0,count=0,lastUpdated=<null>,lastUpdatedTimestamp=<null>],byInstanceType={}], repository=com.atlassian.jira.plugin.devstatus.rest.SummaryItemBean@35c2a9a2[overall=com.atlassian.jira.plugin.devstatus.summary.beans.CommitOverallBean@2568cfa3[count=0,lastUpdated=<null>,lastUpdatedTimestamp=<null>],byInstanceType={}], branch=com.atlassian.jira.plugin.devstatus.rest.SummaryItemBean@40c4eee[overall=com.atlassian.jira.plugin.devstatus.summary.beans.BranchOverallBean@4131b89[count=0,lastUpdated=<null>,lastUpdatedTimestamp=<null>],byInstanceType={}]},errors=[],configErrors=[]], devSummaryJson={\"cachedValue\":{\"errors\":[],\"configErrors\":[],\"summary\":{\"pullrequest\":{\"overall\":{\"count\":0,\"lastUpdated\":null,\"stateCount\":0,\"state\":\"OPEN\",\"details\":{\"openCount\":0,\"mergedCount\":0,\"declinedCount\":0,\"total\":0},\"open\":true},\"byInstanceType\":{}},\"build\":{\"overall\":{\"count\":0,\"lastUpdated\":null,\"failedBuildCount\":0,\"successfulBuildCount\":0,\"unknownBuildCount\":0},\"byInstanceType\":{}},\"review\":{\"overall\":{\"count\":0,\"lastUpdated\":null,\"stateCount\":0,\"state\":null,\"dueDate\":null,\"overDue\":false,\"completed\":false},\"byInstanceType\":{}},\"deployment-environment\":{\"overall\":{\"count\":0,\"lastUpdated\":null,\"topEnvironments\":[],\"showProjects\":false,\"successfulCount\":0},\"byInstanceType\":{}},\"repository\":{\"overall\":{\"count\":0,\"lastUpdated\":null},\"byInstanceType\":{}},\"branch\":{\"overall\":{\"count\":0,\"lastUpdated\":null},\"byInstanceType\":{}}}},\"isStale\":true}}"}}]}

So I want to use Regex to get the content inside devSummaryJson={} block

I have try with devSummaryJson={.* but it return a few extra characters

devSummaryJson={\"cachedValue\":{\"errors\":[],\"configErrors\":[],\"summary\":{\"pullrequest\":{\"overall\":{\"count\":1,\"lastUpdated\":\"2022-12-19T09:09:46.366 0100\",\"stateCount\":1,\"state\":\"MERGED\",\"details\":{\"openCount\":0,\"mergedCount\":1,\"declinedCount\":2,\"total\":3},\"open\":false},\"byInstanceType\":{\"stash\":{\"count\":1,\"name\":\"Bitbucket Server\"}}},\"build\":{\"overall\":{\"count\":0,\"lastUpdated\":null,\"failedBuildCount\":0,\"successfulBuildCount\":0,\"unknownBuildCount\":0},\"byInstanceType\":{}},\"review\":{\"overall\":{\"count\":0,\"lastUpdated\":null,\"stateCount\":0,\"state\":null,\"dueDate\":null,\"overDue\":false,\"completed\":false},\"byInstanceType\":{}},\"deployment-environment\":{\"overall\":{\"count\":0,\"lastUpdated\":null,\"topEnvironments\":[],\"showProjects\":false,\"successfulCount\":0},\"byInstanceType\":{}},\"repository\":{\"overall\":{\"count\":1,\"lastUpdated\":\"2022-12-19T09:09:46.000 0100\"},\"byInstanceType\":{\"stash\":{\"count\":1,\"name\":\"Bitbucket Server\"}}},\"branch\":{\"overall\":{\"count\":0,\"lastUpdated\":null},\"byInstanceType\":{}}}},\"isStale\":false}}"}}]}.

So Is there any way to solve this problem? (Removing exactly three specific characters at the end is not preferred, due to in the future maybe have change the json format)

Many thanks.

CodePudding user response:

The simplest regex: "devSummaryJson={.*(?=}]})"gm

regex101.com

  • (?=}]}) is a positive lookahead. It searches everything before three specific characters }]}.
  • devSummaryJson={.* the position where the search starts.

Anyway, regex is not applicable for this type of problem and the best solution would be using a json parser.

CodePudding user response:

Thanks for your support. I have find the solution as your suggestion.

First: parser the json to the string [1]: https://i.stack.imgur.com/Ybvws.png

Second: Use Regex to get the content devSummaryJson [2]: https://i.stack.imgur.com/v6rA4.png

I'll try with groovy and post in here for anyone need.

  • Related