Can I use a loop to construct the response body wh...
# help
v
Can I use a loop to construct the response body when composing the response for a request using raw JSON? Below is my attempt.
Copy code
"arrayName": [
    "{{#each (jsonPath request.body '$.[0].arrayName') as |node|}}",
    {
    	"type": "{{node.type}}",
    	"value": "{{node.value}}"
    },
    "{{/each}}"
]
But this result is like that.
Copy code
"arrayName": [
    "",
    {
        "type": "aaa",
        "value": "bbb"
    }
    "",
    {
        "type": "ccc",
        "value": "ddd"
    },
    ""
]
l
I don't think you will need the
"
around the
each
template part
You will also need to manage the
,
on the last object in the array because if it is there it won't be valid json
Maybe something like this:
Copy code
"arrayName": [
    {{#each (jsonPath request.body '$.[0].arrayName') as |node|}}
    {
    	"type": "{{node.type}}",
    	"value": "{{node.value}}"
    }{{#not @last}},{{/not}}  
    {{/each}}
]
The only problem with this type of response file is that it isn't valid json so won't play nice with most editors. It should generate valid json though
v
Thanks for your suggestion. But if I remove the
"
, the Wiremock server(I used a standalone server) occurs error.
Copy code
wiremock  | Exception in thread "main" com.github.tomakehurst.wiremock.standalone.MappingFileException: Error loading file /home/wiremock/./mappings/NAC/post-nimbus-servicesapidedupe-1ef9c420-34f4-4e35-8f0a-501adab822bb.json:
wiremock  | Unexpected character ('{' (code 123)): was expecting double-quote to start field name
wiremock  |  at [Source: (byte[])"{
wiremock  |         }"[truncated 2599 bytes]; line: 58, column: 26]
wiremock  | 	at com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:145)
wiremock  | 	at com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:294)
wiremock  | 	at com.github.tomakehurst.wiremock.core.WireMockApp.loadDefaultMappings(WireMockApp.java:287)
wiremock  | 	at com.github.tomakehurst.wiremock.core.WireMockApp.<init>(WireMockApp.java:143)
wiremock  | 	at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:74)
wiremock  | 	at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:72)
wiremock  | 	at wiremock.Run.main(Run.java:23)
l
Do you have response templating enabled ?
v
Sure, is it correct?
Copy code
"transformers": [
    "response-template"
]
l
Would you be able to copy your mapping file so I can take a look at the whole thing and test
v
Here is the entire JSON.
Copy code
{
  "uuid": "1ef9c420-34f4-4e35-8f0a-501adab822bb",
  "persistent": true,
  "request": {
    "urlPathPattern": "/test/url",
    "method": "POST",
    "headers": {
      "Accept": {
        "contains": "application/json"
      }
    },
    "bodyPatterns": [
      {
        "matchesJsonSchema": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "paramA": {
                "type": "string",
                "pattern": "[0-9]+"
              },
              "paramB": {
                "type": "string",
                "pattern": "[0-9]{6}"
              },
              "paramC": {
                "type": "string",
                "pattern": "[0-9]{10}"
              },
              "paramD": {
                "type": "string"
              },
              "paramE": {
                "type": "string"
              },
              "paramF": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "type": {
                      "type": "string"
                    },
                    "value": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "jsonBody": [
      {
        "dedupeRequestSource": {
          "paramA": "{{jsonPath request.body '$.[0].paramA'}}",
          "paramB": "{{jsonPath request.body '$.[0].paramB'}}",
          "paramC": "{{jsonPath request.body '$.[0].paramC'}}",
          "paramD": "{{jsonPath request.body '$.[0].paramD'}}",
          "paramF": [
            "{{#each (jsonPath request.body '$.[0].paramF') as |node|}}",
            {
              "type": "{{node.type}}",
              "value": "{{node.value}}"
            },
            "{{/each}}"
          ],
          "paramE": "{{jsonPath request.body '$.[0].paramE'}}"
        },
        "results": [],
        "type": "PRIMARY",
        "dedupeReferenceId": "{{randomValue length=16 type='NUMERIC'}}",
        "dedupePresent": false,
        "invalidDedupeResponse": false
      }
    ],
    "headers": {
      "Content-Type": "application/json"
    },
    "transformers": [
      "response-template"
    ]
  }
}
l
Ah, cool. So, the mapping file needs to be valid JSON which is why you are having to put the
"
round everything. I think the example I gave you is correct but you will have to use
body
instead of
jsonBody
which unfortunately means you will have to escape everything in the response body. The other option is to pull out the response body into a separate file and reference it via the
bodyFileName
element. You can find more about that here - https://wiremock.org/docs/response-templating/
v
Thank you so much. I will try again by referring document you mentioned.
l
Let me know how you get on
🫡 1
v
Amazing!!! It works fine!! I really appreciate your assistance.
l
No worries. Glad it helped
🙇‍♂️ 1