1
votes

Though I could see this question might be repeated but couldn't find any similar solution for the below JSON strut. Pls suggest.

I have excel sheet where the data's in columns look like : CSV file data

My expected JSON as:

       {
       "Child ": {
       "10"
          : { "Post": { "Kid-R":1 },
        "Var": [1,1 ],
        "Tar": [2,2],
        "Fur": [3,3]},

       "11": 
          {"Post": {"Kid-R":2 },
          "Var": [1,1 ],
          "Tar": [2,2 ],
          "Fur": [5,4 ]}
          },
         "Clone": [],
         "Birth": 2,
         "TT": 11,
         "Clock": ${__time(/1000,)}
           }

I have tried incorporating beanshell preprocessor in JMeter & tried below code:

    def builder = new groovy.json.JsonBuilder()
    @groovy.transform.Immutable
    class Child {
    String post
    String var
    String Tar
    String Fur
    }


    def villas = new File("Audit_27.csv")
    .readLines()
    .collect { line ->
        new child (line.split(",")[1],(line.split(",") 
    [2]+","+line.split(",")[3]),(line.split(",")[4]+","+line.split(",") 
   [5]),(line.split(",")[6]+","+line.split(",")[7]))}     

    builder(

    Child :villas.collect(),
         "Clone": [],
         "Birth": 2,
         "TT": 11,
         "Clock": ${__time(/1000,)}

    )
     log.info(builder.toPrettyString())
     vars.put("payload", builder.toPrettyString())

And I could see below response only:

Note: I dont know how to declare "Key" value (line.split(",")[0]) in the above solution.

      {
     "Child": [
      {
        "post": "\"\"\"Kid-R\"\":1\"",
        "var": "\"[2,2]\"",
        "Tar": "\"[1,1]\"",
        "Fur": "\"[3,3]\""
    },
    {
        "post": "\"\"\"Kid-R\"\":2\"",
        "var": "\"[2,2]\"",
        "Tar": "\"[1,1]\"",
        "Fur": "\"[3,3]\""
      }
     ],
     "Clone": [],
     "Birth": 2,
     "TT": 11,
     "CLock": 1585219797
      }

Any help would be greatly appreciated

2

2 Answers

0
votes

You're copying and pasting the solution from this answer without understanding what you're doing.

If you change class name from VILLA to own you need to use new own instead of new VILLA

Also this line won't compile: Clock: <take system current time> you need to use System.currentTimeMillis() or appropriate function of the Date class in order to generate the timestamp.

If you want a comprehensive answer, you need to provide:

  1. Well-formatted CSV file
  2. Valid JSON payload

In the meantime I would recommend getting familiarized with the following material:

  1. Apache Groovy: Parsing and producing JSON
  2. Apache Groovy - Why and How You Should Use It
  3. Reading a File in Groovy
0
votes

Actually I am gonna follow DmirtiT suggestions, as mentioned in some of post to use random variable for bulk API request. Same answer it helped me here as well to generate multiple JSON structure with unique data. Thanks..