Home > Enterprise >  How to save json response from API call into json files?
How to save json response from API call into json files?

Time:08-04

Running through lots of files and response is also in thousands. How do I save the json response with names similar to the name of files I sent as request? Is there any way? For example if I read a file with name"arnold.json" I want to take the response and write it to a file with name: "arnold_response.json"(something like this) The response I am getting looks like this:

{
  "resourceType": "Bundle",
  "id": "235-253-235-235-235",
  "type": "transaction-response",
  "link": [
    {
      "relation": "self",
      "url": "https://someurl.com"
    }
  ],
  "entry": [
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    },
   {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    }
  ]
}

image of how my setup looks: https://ibb.co/MB46z6z

CodePudding user response:

You can add a JSR223 Listener and use the following code there:

new File(vars.get('file')).bytes = prev.getResponseData()

where:

More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

CodePudding user response:

In addition to Dimitri's answer (which is very good), I also found this article online which offers a few more options to achieve your goal for diversity's sake :)

https://www.baeldung.com/jmeter-write-to-file

Hope it helps. Cheers!

  • Related