38626

JSON with duplicate key names losing information when parsed

So either I go back and tell someone that they should fix their JSON, or I need to find out what I am doing wrong. Here is the JSON, notice that <strong>parameter</strong> occurs three times:

String j= '''{ "jobname" : "test", "parameters" : { "parameter": {"name":"maxErrors", "value":"0"}, "parameter": {"name":"case", "value":"lower"}, "parameter": {"name":"mapTable", "value":"1"} } } '''

And I am trying to get each name & value. My code

def doc = new JsonSlurper().parseText(j) def doc1 = doc.entrySet() as List def doc2 = doc.parameters.entrySet() as List println "doc1.size===>"+doc1.size() println "doc1===>"+doc1 println "doc2.size===>"+doc2.size() println "doc2===>"+doc2

And my results:

doc1.size===>2 doc1===>[jobname=test, parameters={parameter={name=mapTable, value=1}}] doc2.size===>1 doc2===>[parameter={name=mapTable, value=1}]

How come I only get one parameter? Where are the other two? It looks like JSON only keeps one <strong>parameter</strong> and discards the other ones.

Answer1:

The JSON is not in the correct format. There should not be duplicate key in the same hierarchy or they will override each other.

It should have been an array of paramters.

Like this,

String j= '''{ "jobname" : "test", "parameters" : [ {"name":"maxErrors", "value":"0"}, {"name":"case", "value":"lower"}, {"name":"mapTable", "value":"1"} ] }

Recommend

  • groovy.json.JsonSlurper parse JSON
  • Transfer Property ids (Array) to other TestCases in SoapUI/Groovy
  • Referencing groovy variable as part of JSON path
  • Grails calculated field in SQL
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • How to attach a node.js readable stream to a Sendgrid email?
  • Python urlparse: small issue
  • Yii2: Config params vs. const/define
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Display issues when we change from one jquery mobile page to another in firefox
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Deselecting radio buttons while keeping the View Model in synch
  • Spring security and special characters
  • How to add date and time under each post in guestbook in google app engine
  • Join two tables and save into third-sql
  • Display Images one by one with next and previous functionality
  • How to make Safari send if-modified-since header?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Jquery - Jquery Wysiwyg return html as a string
  • Matplotlib draw Spline from multiple points
  • Return words with double consecutive letters
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • How to pass list parameters for each object using Spring MVC?
  • Circular dependency while pushing http interceptor
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • Linker errors when using intrinsic function via function pointer
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • C# - Getting references of reference
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • How can I remove ASP.NET Designer.cs files?
  • JaxB to read class hierarchy
  • How to Embed XSL into XML
  • java string with new operator and a literal