alexmark
alexmark

Reputation: 391

Regex to extract fields in json NiFi

Beginner RegExp question. I have a JSON in my NiFi ExtractText and there are 2 fields I want to extract. How would I use a regex to do this?

[
  {
    "id": "12erf3-312331-233"
  },
  [
    {
      "id": "1234",
      "id2": "1234",
      "id3": "1234"
    },
    {
      "id": "1234",
      "id2": "1234",
      "id3": "1234"
    },
    {
      "id": "1234",
      "id2": "1234",
      "id3": "1234"
    },
    {
      "id": "1234",
      "id2": "1234",
      "id3": "5555"
    }
  ]
]
  1. get the first id: "12erf3-312331-233" (this is a uuid)
  2. get all the second array: [ { "id": "1234" ... "id3": "5555" } ]

Upvotes: 0

Views: 383

Answers (1)

YuriR
YuriR

Reputation: 1311

To achieve what you need you may use ReplaceText processor. Have a look to the configuration below. Search Value would be (?s).*("id": )(".*").*}.*(\[.*\]).*\] and the Replacement Value: {$1$2,data:$3}. Pay attention to pick Evaluation Mode as "Entire text". To easily check regex you can use link i have used: https://regex101.com/r/p6T2Vw/1

enter image description here

Upvotes: 3

Related Questions