Have a sample JSON like below which has duplicate keys with field context :
{
"Production": {
"meta_id": "1239826",
"endeca_id": "EN29826",
"Title": "Use The Google Home ™ To Choose The Right CCCM Solution For Your Needs",
"Subtitle": null,
"context": {
"researchID": "22",
"researchtitle": " The Google Home ™: Cross-Channel , Q4 2019",
"telconfdoclinkid": null
},
"context": {
"researchID": "281",
"researchtitle": " The Google Home ™: Cross-Channel Q3 2019",
"telconfdoclinkid": null
},
"context": {
"researchID": "154655",
"researchtitle": " Now Tech: Cross-Channel Campaign Management, Q2 2019",
"telconfdoclinkid": null
},
"uri": "/doc/uri",
"ssd": "ihdfiuhdl",
"id": "dsadfsd221e"
}
}
When I am parsing the JSON for field "context" in scala , it's reject the JSON with a parsing error as below.
Exception in thread "main" org.json.JSONException: Duplicate key "context".
Could you suggest best approach to parse a json in above format using scala.
Some JSON parsers for Scala that parse from JSON bytes to your data structures can parse duplicated keys using custom codecs.
Below is an example how it can be done with jsoniter-scala:
Add dependencies to your
build.sbt:Use data structures and the custom codec from the following snippet:
Expected output: