JMETER – 使用JsonBuilder格式化JSON

huangapple go评论131阅读模式
英文:

JMETER - Formatting JSON with JsonBuilder

问题

抱歉,我无法访问外部链接或执行代码,但我可以为您提供如何在现有的Groovy代码中将所需的对象添加到最终JSON中的指导:

要将额外的对象添加到最终JSON中,您可以在生成books数组后,创建一个包含您所需对象的Map,并将其添加到最终的JSON结构中。以下是一种修改代码的方法:

  1. def arraySize = vars.entrySet().findAll { entry -> entry.getKey().startsWith('isbn_') }.size()
  2. def books = []
  3. 1.upto(arraySize, {index ->
  4. books.add([publisher: vars.get('publisherName'),
  5. ISBN: vars.get('isbn_' + index),
  6. Price: vars.get('price_' + index),
  7. Publisher: vars.get('publisher_' + index),
  8. "Static Value that doesn't change": 100
  9. ])
  10. })
  11. // 创建您要添加的额外对象
  12. def extraObject = [
  13. publisher: vars.get('publisherName'), // 这里使用与数组中相同的值
  14. ISBN: vars.get('your_extra_isbn'), // 替换为您的额外ISBN变量
  15. Price: vars.get('your_extra_price'), // 替换为您的额外价格变量
  16. Publisher: vars.get('your_extra_publisher'), // 替换为您的额外出版商变量
  17. "Static Value that will be the same for all items": 100
  18. ]
  19. // 创建最终的JSON结构,将额外对象添加到books数组后面
  20. def finalJson = new groovy.json.JsonBuilder([
  21. books: books + [extraObject], // 将额外对象添加到数组末尾
  22. meta: [
  23. tag: [
  24. searchNumber: vars.get('searchNo')
  25. ]
  26. ]
  27. ]).toPrettyString()
  28. log.info(finalJson)

请注意,上述代码中的your_extra_isbnyour_extra_priceyour_extra_publisher 应替换为您要添加的额外对象的变量名称。这将创建一个包含额外对象的最终JSON,并在books数组的末尾添加它。

英文:

Hello I previously asked how to dynamically generate a json array using Groovy here:

https://stackoverflow.com/questions/76569179/jmeter-populate-a-json-request-for-all-items-in-an-array

The provided solution worked great, however I need to add an additional object after the final item in the array. Because the toprettystring formats the array I am unsure how to insert the object into the final result.

Here is an example of the final JSON:

  1. {
  2. "books": [
  3. {
  4. "publisher": ${publisherName}, //variable but it's the same for all items in this request
  5. "ISBN": ${isbn_1}, //the next three are from from 3 different arrays
  6. "Price": ${price_1},
  7. "Publisher": ${publisher_1},
  8. "Static Value that doesn't change": 100
  9. },
  10. {
  11. "publisher": ${publisherName}, //variable but it's the same for all items in this request
  12. "ISBN": ${isbn_2}, //the next three are from from 3 different arrays
  13. "Price": ${price_2},
  14. "Publisher": ${publisher_2},
  15. "Static Value that will be the same for all items": 100
  16. },
  17. ],
  18. "meta": {
  19. "tag": {
  20. "searchNumber": ${searchNo}
  21. }
  22. }
  23. }

The solution for the previous question was to use a JSR223 Preprocessor and generate it as follows:

  1. def arraySize = vars.entrySet().findAll { entry -> entry.getKey().startsWith('isbn_') }.size()
  2. def books = []
  3. 1.upto(arraySize, {index ->
  4. books.add([publisher:vars.get('publisherName'),
  5. ISBN: vars.get('isbn_' + index),
  6. Price: vars.get('price_' + index),
  7. Publisher:vars.get('publisher_' + index),
  8. "Static Value that doesn't change": 100
  9. ])
  10. })
  11. log.info(new groovy.json.JsonBuilder([books:books]).toPrettyString())

How could I modify this to enter the above object into the final JSON?

答案1

得分: 1

你的最终结果在这里形成:

  1. log.info(new groovy.json.JsonBuilder([books: books]).toPrettyString())

只需将所需字段添加到此映射中:[books: books]

例如,替换最后一行:

  1. def searchNo = ...
  2. def result = [
  3. books: books,
  4. meta: [
  5. tag: [searchNumber: searchNo]
  6. ]
  7. ]
  8. log.info(new groovy.json.JsonBuilder(result).toPrettyString())
英文:

your final result formed here:

  1. log.info(new groovy.json.JsonBuilder([books:books]).toPrettyString())

just add required fields into this map: [books:books]

for example instead of last line:

  1. def searchNo = ...
  2. def result=[
  3. books: books,
  4. meta: [
  5. tag: [searchNumber: searchNo]
  6. ]
  7. ]
  8. log.info(new groovy.json.JsonBuilder(result).toPrettyString())

答案2

得分: 1

请按以下方式修改您的代码:

  1. def arraySize = vars.entrySet().findAll { entry -> entry.getKey().startsWith('isbn_') }.size()
  2. def request = [:]
  3. def books = []
  4. 1.upto(arraySize, { index ->
  5. books.add([publisher : vars.get('publisherName'),
  6. ISBN : vars.get('isbn_' + index),
  7. Price : vars.get('price_' + index),
  8. Publisher : vars.get('publisher_' + index),
  9. "Static Value that doesn't change": 100
  10. ])
  11. })
  12. def meta = [tag: [searchNumber: vars.getObject('searchNo')]]
  13. request.put('books', books)
  14. request.put('meta', meta)
  15. log.info((new groovy.json.JsonBuilder(request).toPrettyString()))

更多信息:

  1. <details>
  2. <summary>英文:</summary>
  3. Amend your code as follows:
  4. def arraySize = vars.entrySet().findAll { entry -&gt; entry.getKey().startsWith(&#39;isbn_&#39;) }.size()
  5. def request = [:]
  6. def books = []
  7. 1.upto(arraySize, { index -&gt;
  8. books.add([publisher : vars.get(&#39;publisherName&#39;),
  9. ISBN : vars.get(&#39;isbn_&#39; + index),
  10. Price : vars.get(&#39;price_&#39; + index),
  11. Publisher : vars.get(&#39;publisher_&#39; + index),
  12. &quot;Static Value that doesn&#39;t change&quot;: 100
  13. ])
  14. })
  15. def meta = [tag: [searchNumber: vars.getObject(&#39;searchNo&#39;)]]
  16. request.put(&#39;books&#39;, books)
  17. request.put(&#39;meta&#39;, meta)
  18. log.info((new groovy.json.JsonBuilder(request).toPrettyString()))
  19. More information:
  20. - [Apache Groovy: Parsing and producing JSON][1]
  21. - [Apache Groovy: What Is Groovy Used For?][2]
  22. [1]: https://groovy-lang.org/processing-json.html
  23. [2]: https://www.blazemeter.com/blog/apache-groovy
  24. </details>

huangapple
  • 本文由 发表于 2023年7月18日 03:00:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定