user18649321
user18649321

Reputation: 11

How to create json array and values using Groovy jsonBiulder

trying to create the below json structure with groovy jsonBuilder in jmeter using JSR223 sampler

{
   "Catalogues":[
      {
         "Catalogue":{
            "itemName":"XYZ",
            "Level":"Class1",
            "Color":"Orange",
            "Id":"232f2d6820822840"
         },
         "sales":[
            [
               "7:19 PM 3-31-2022",
               "gadfma53742w3585657er43"
            ],
            [
               "5:02 PM 3-30-2022",
               "iytyvh53742w3585657er41"
            ]
         ]
      }
   ]
}

I have tried the below groovy script


def json = new groovy.json.JsonBuilder()
json {
  Catalogues(
    [{
      Catalogue {
        itemName('XYZ')
        Level('Class1')
        Color('Orange')
        Id('232f2d6820822840')
      },
      {
        sales(

          ('7:19 PM 3-31-2022'), ('gadfma53742w3585657er43')

        )
      }

    }]
  )
}

log.info '\n\n\n' + json.toPrettyString() + '\n\n\n'    

Output:

{
   "Catalogues":[
      {
         "Catalogue":[
            {
               "itemName":"XYZ",
               "Level":"Class1",
               "Color":"Orange",
               "Id":"232f2d6820822840"
            },
            {
               "sales":[
                  "7:19 PM 3-31-2022",
                  "gadfma53742w3585657er43"
               ]
            }
         ]
      }
   ]
}

Problems:

  1. If I remove the '{' before sales and after (at corresponding location), it adds sales values into catalogue
  2. unable to include second set of sales values

Upvotes: 1

Views: 1381

Answers (2)

daggett
daggett

Reputation: 28564

I'm suggesting another way to use builder because it's easier to understand.

To declare array in groovy use [1, 2, 3]

To declare map [a:1, b:2, c:3]

So, if you replace in original json { to [ - you will get valid groovy object that corresponds to parsed json

def data = [
   "Catalogues":[
      [
         "Catalogue":[
            "itemName":"XYZ",
            "Level":"Class1",
            "Color":"Orange",
            "Id":"232f2d6820822840"
         },
         "sales":[
            [
               "7:19 PM 3-31-2022",
               "gadfma53742w3585657er43"
            ],
            [
               "5:02 PM 3-30-2022",
               "iytyvh53742w3585657er41"
            ]
         ]
      ]
   ]
]

//then you could convert it to json:
def json = new groovy.json.JsonBuilder(data).toPrettyString()
log.info(json)

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 167992

JsonBuilder translates Map to JSON Object and List to JSON Array

So I would recommend for better readability and clarity amending your code to look like:

def body = [:]

def Catalogues = []

def Catalogue = [:]

def entry = [:]

Catalogue.put('itemName', 'XYZ')
Catalogue.put('Level', 'Class1')
Catalogue.put('Color', 'Orange')
Catalogue.put('Id', '232f2d6820822840')

def sales = []

sales.add(['7:19 PM 3-31-2022', 'gadfma53742w3585657er43'])
sales.add(['5:02 PM 3-30-2022', 'iytyvh53742w3585657er41'])

entry.put('Catalogue', Catalogue)
entry.put('sales', sales)

Catalogues.add(entry)

body.put('Catalogues', Catalogues)

def json = new groovy.json.JsonBuilder(body)

More information:

Upvotes: 0

Related Questions