admin管理员组

文章数量:1336632

I'm expected to read the value of where(key = Canonical message) and the value format is combination of many fields where I expected the read 'casetTitle' value in the Payload section mentioned below and set a property using JsonSlurper()

Input JsoN
    {
        "Id": "db338737-af14-ef11-9f8a-000d3aad8ef7",
        "InputParameters": [
            {
           
                "key": "CanonicalMessage",
                "value": "{\"ActivityId\":\"9f368268\",\"Payload\":{\"CaseTitle\":\"Material Description 000\",\"CustomerAssetNumber\":\"104628\"}}"
            }
            
            {
                   "key": "transactionlogid",
                    "value": "9f36828e9"
            }
        ]
        
         }

how this be achieved through using JsonSlurper()?

Thank you very much

Yours sincerely Sateesh

I'm expected to read the value of where(key = Canonical message) and the value format is combination of many fields where I expected the read 'casetTitle' value in the Payload section mentioned below and set a property using JsonSlurper()

Input JsoN
    {
        "Id": "db338737-af14-ef11-9f8a-000d3aad8ef7",
        "InputParameters": [
            {
           
                "key": "CanonicalMessage",
                "value": "{\"ActivityId\":\"9f368268\",\"Payload\":{\"CaseTitle\":\"Material Description 000\",\"CustomerAssetNumber\":\"104628\"}}"
            }
            
            {
                   "key": "transactionlogid",
                    "value": "9f36828e9"
            }
        ]
        
         }

how this be achieved through using JsonSlurper()?

Thank you very much

Yours sincerely Sateesh

Share Improve this question asked Nov 20, 2024 at 19:36 SateeshSateesh 531 silver badge9 bronze badges 1
  • 1 Please add the code you have tried and how it failed (e.g. errors, stacktraces, logs, ...) so we can improve on it. – cfrick Commented Nov 20, 2024 at 19:46
Add a comment  | 

1 Answer 1

Reset to default 0

It'd look something like the following. Keep in mind I had to escape the quote escape sequence because I inlined this into a String literal. Otherwise the language was interpret \" -> " and create invalid JSON. If you were reading this from input you wouldn't need to do that. This is just an artifact of this example; not real world code.

import groovy.json.*

def slurper = new JsonSlurper()
def json = slurper.parseText("""
   {
        "Id": "db338737-af14-ef11-9f8a-000d3aad8ef7",
        "InputParameters": [
            {
                "key": "CanonicalMessage",
                "value": "{\\"ActivityId\\":\\"9f368268\\",\\"Payload\\":{\\"CaseTitle\\":\\"Material Description 000\\",\\"CustomerAssetNumber\\":\\"104628\\"}}"
            },
            {
                "key": "transactionlogid",
                "value": "9f36828e9"
            }
        ]
        
    }
""")

List<String> caseTitles = json.InputParameters
   .findAll { it.key == "CanonicalMessage" }
   .collect { 
      def innerJson = slurper.parseText( it.value )
      innerJson.Payload.CaseTitle
   }

println(caseTitles)

This is executable and works.

本文标签: groovyParse value from JSON Input message with special char formatStack Overflow