Scala PlayFrameWork-如何根据属性值修改JSON和删除元素表单JSarray(seq [jsobject])

发布于 2025-02-07 22:09:50 字数 1836 浏览 1 评论 0原文

我有一个类似的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。

  1. 我创建了一个可变的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.

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2025-02-14 22:09:50

如果带有processes键的对象未嵌套在数组中,则可以使用JSON Transformers:

import play.api.libs.json._

val inputJsonStr = """{
    "id": 1001,
    "processes": [
        {
            "process_id": 301,
            "status": "accepted"
        },
        {
            "process_id": 302,
            "status": "accepted"
        },
        {
            "process_id": 303,
            "status": "failed"
        },
        {
            "process_id": 304,
            "status": "failed"
        }
    ]
}"""
val inputJsonObj = Json.parse(inputJsonStr)

val transformer = (__ \ "processes").json.update(
  __.read[JsArray].map { case JsArray(arr) =>
    JsArray(
      arr.filter(
        info => (info \ "status").as[String].equals("accepted")
      )
    )
  }
)

inputJsonObj
  .transform(transformer)
  .map(Json.prettyPrint)

请参阅 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:

import play.api.libs.json._

val inputJsonStr = """{
    "id": 1001,
    "processes": [
        {
            "process_id": 301,
            "status": "accepted"
        },
        {
            "process_id": 302,
            "status": "accepted"
        },
        {
            "process_id": 303,
            "status": "failed"
        },
        {
            "process_id": 304,
            "status": "failed"
        }
    ]
}"""
val inputJsonObj = Json.parse(inputJsonStr)

val transformer = (__ \ "processes").json.update(
  __.read[JsArray].map { case JsArray(arr) =>
    JsArray(
      arr.filter(
        info => (info \ "status").as[String].equals("accepted")
      )
    )
  }
)

inputJsonObj
  .transform(transformer)
  .map(Json.prettyPrint)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文