简体   繁体   中英

how to remove attributes from json using Kotlin and jackson ObjectMapper

I want to remove all occurrence of "attributeToRemove" in the following JSON:

{
    "Item994": [
        {
            "attributeToRemove": {
                "someItem": null
            },
            "types": [
                "type194",
                "type294"
            ],
            "p1": "SOS"
        }
    ],
    "Item99": [
        {
            "attributeToRemove": {
                "someItem": null
            },
            "types": [
                "type19",
                "type29"
            ],
            "p1": "SOS"
        }
    ]
}

I tried using removeAll but I keep this Error: Type mismatch: inferred type is (JsonNode!) -> JsonNode! but (JsonNode!) -> Boolean was expected Type mismatch: inferred type is (JsonNode!) -> JsonNode! but (JsonNode!) -> Boolean was expected

Can anyone suggest how to fix this?

Here's my code:

import com.fasterxml.jackson.databind.ObjectMapper

    fun main ( args : Array < String > ) {

        val someString = "{\n" +
                "    \"Item994\": [\n" +
                "        {\n" +
                "            \"attributeToRemove\": {\n" +
                "                \"someItem\": null\n" +
                "            },\n" +
                "            \"types\": [\n" +
                "                \"type194\",\n" +
                "                \"type294\"\n" +
                "            ],\n" +
                "            \"p1\": \"SOS\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"Item99\": [\n" +
                "        {\n" +
                "            \"attributeToRemove\": {\n" +
                "                \"someItem\": null\n" +
                "            },\n" +
                "            \"types\": [\n" +
                "                \"type19\",\n" +
                "                \"type29\"\n" +
                "            ],\n" +
                "            \"p1\": \"SOS\"\n" +
                "        }\n" +
                "    ]\n" +
                "}"
        val mapper = ObjectMapper()
        val jsonStr = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(someString)

        val jsonResult = mapper.readTree(someString)
        jsonResult.removeAll { it.get("attributeToRemove") }

    }

convert jsonString to an Object, ignore the fields you want to remove then map back to a jsonString:

import com.google.gson.Gson
import com.google.gson.annotations.SerializedName

class Item99 {
    var p1: String? = null
    var types: Array<String>? = null
}

class Item994 {
    var p1: String? = null
    var types: Array<String>? = null
}

class Wrapper (
        @SerializedName("Item99")
        var item99: Array<Item99>?,
        @SerializedName("Item994")
        var item994: Array<Item994>?
)

object Main {

    var jsonString = "{\n" +
            "    \"Item994\": [\n" +
            "        {\n" +
            "            \"attributeToRemove\": {\n" +
            "                \"someItem\": null\n" +
            "            },\n" +
            "            \"types\": [\n" +
            "                \"type194\",\n" +
            "                \"type294\"\n" +
            "            ],\n" +
            "            \"p1\": \"SOS\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"Item99\": [\n" +
            "        {\n" +
            "            \"attributeToRemove\": {\n" +
            "                \"someItem\": null\n" +
            "            },\n" +
            "            \"types\": [\n" +
            "                \"type19\",\n" +
            "                \"type29\"\n" +
            "            ],\n" +
            "            \"p1\": \"SOS\"\n" +
            "        }\n" +
            "    ]\n" +
            "}"

    @JvmStatic
    fun main(args: Array<String>) {
        val gson = Gson()
        val wrapper = gson.fromJson(jsonString,Wrapper::class.java)
        println(gson.toJson(wrapper))

    }
}

The output will look like this:

{"Item99":[{"p1":"SOS","types":["type19","type29"]}],"Item994":[{"p1":"SOS","types":["type194","type294"]}]}

import com.fasterxml.jackson.annotation.JsonAutoDetect
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper


@JsonIgnoreProperties(ignoreUnknown = true)
class Item99 {
    var p1: String? = null
    var types: Array<String>? = null
}

@JsonIgnoreProperties(ignoreUnknown = true)
class Item994 {
    var p1: String? = null
    var types: Array<String>? = null
}

class Wrapper {
    @JsonProperty("Item99")
    var item99: Array<Item99>? = null

    @JsonProperty("Item994")
    var item994: Array<Item994>? = null
}

object Main {

    var jsonString = "{\n" +
            "    \"Item994\": [\n" +
            "        {\n" +
            "            \"attributeToRemove\": {\n" +
            "                \"someItem\": null\n" +
            "            },\n" +
            "            \"types\": [\n" +
            "                \"type194\",\n" +
            "                \"type294\"\n" +
            "            ],\n" +
            "            \"p1\": \"SOS\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"Item99\": [\n" +
            "        {\n" +
            "            \"attributeToRemove\": {\n" +
            "                \"someItem\": null\n" +
            "            },\n" +
            "            \"types\": [\n" +
            "                \"type19\",\n" +
            "                \"type29\"\n" +
            "            ],\n" +
            "            \"p1\": \"SOS\"\n" +
            "        }\n" +
            "    ]\n" +
            "}"

    @JvmStatic
    fun main(args: Array<String>) {
        val mapper = ObjectMapper()
        mapper.visibilityChecker = mapper.serializationConfig.defaultVisibilityChecker.withCreatorVisibility(JsonAutoDetect.Visibility.ANY)
        val answer = mapper.readValue(jsonString, Wrapper::class.java)
        print(mapper.writeValueAsString(answer))
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM