Scala PlayFrameWork-如何根据属性值修改JSON和删除元素表单JSarray(seq [jsobject])
我有一个类似的json文件。
{
"data": [
{
"id": 1001,
"processes": [
{
"process_id": 301,
"status": "accepted"
},
{
"process_id": 302,
"status": "accepted"
},
{
"process_id": 303,
"status": "failed"
},
{
"process_id": 304,
"status": "failed"
}
]
}
]
}
我想通过JSON迭代并删除所有状态失败的过程, 因此,我的修改后的JSON应该是
{
"data": [
{
"id": 1001,
"processes": [
{
"process_id": 301,
"status": "accepted"
},
{
"process_id": 302,
"status": "accepted"
}
]
}
]
}
我尝试使用scalajsontransformers,但修剪和更新对Jsobject的作品,而不是Jsarray上的作品。
我确实尝试使用
1)
val inputJsonObj = Json.parse(inputJsonStr).as[Seq[JsObject]
val modifiedJson = inputJsonObj.map(model => (model \ "processes").as[Seq[JsObject]].filter(info => {
val status = (info \ "status").as[String]
status match {
case "accepted" => true
case _ =>
// I tried to prune/update here, but its not working
false
}
}))
代码正确过滤,但没有修改我的实际JSON。
- 我创建了一个可变的jsobject,并试图覆盖流程的价值,但这也行不通。
JSON.PARSE(resp).AS [mutable.seq [jSobject]] ++ json.obj(“ processes” - > modifiedjson)
有人可以在此方面提供帮助,如何更新我的json。我想在不使用任何情况类别的情况下修改JSON。
I have a json file something like this.
{
"data": [
{
"id": 1001,
"processes": [
{
"process_id": 301,
"status": "accepted"
},
{
"process_id": 302,
"status": "accepted"
},
{
"process_id": 303,
"status": "failed"
},
{
"process_id": 304,
"status": "failed"
}
]
}
]
}
I want to iterate through the Json and remove all the processes which have failed status,
So my modified Json should be
{
"data": [
{
"id": 1001,
"processes": [
{
"process_id": 301,
"status": "accepted"
},
{
"process_id": 302,
"status": "accepted"
}
]
}
]
}
I have tried with ScalaJsonTransformers but the prune and update works on JsObject, and not on JsArray.
I did try to use
1)
val inputJsonObj = Json.parse(inputJsonStr).as[Seq[JsObject]
val modifiedJson = inputJsonObj.map(model => (model \ "processes").as[Seq[JsObject]].filter(info => {
val status = (info \ "status").as[String]
status match {
case "accepted" => true
case _ =>
// I tried to prune/update here, but its not working
false
}
}))
The code filter out correctly, but does not modify my actual Json.
- I created a mutable JsObject and tried to overwrite the value of Processes but this also does not work.
Json.parse(resp).as[mutable.Seq[JsObject]] ++ Json.obj("processes" -> modifiedJson)
Can someone please help in this, how to update my json. I want to modify the Json without using any case classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果带有
processes
键的对象未嵌套在数组中,则可以使用JSON Transformers:请参阅 https://scastie.scala-lang.org/um08r9qttnsivrs97fwg3a
然而,由于此错误(或难以修改嵌套在阵列中的物体)可能是不可能(或困难),因此可能是不可能的: https://github.com/playframework/playframework/play-json/issues/82 。
If the object with the
processes
key weren't nested inside an array, you could use JSON transformers:See https://scastie.scala-lang.org/um08R9qTTnSIvrs97FWg3A
However, modifying an object nested in an array may be impossible (or difficult) due to this bug: https://github.com/playframework/play-json/issues/82.