Skip to content

How can i rename a field using mapneat, wich is inside list object #27

Answered by nomemory
hudavianto92 asked this question in Q&A
Discussion options

You must be logged in to vote

@hudavianto92

The code that is performing what you are looking for:

val source = """
{
    "detail" : [
        {
            "name" : "huda"
        },
        {
            "name" : "syahnaz"
        }
    ]
    }
"""

fun main() {

    var value = json(fromJson(source)) {
         "detail" *= "$.detail"
        val detail = getObjectMap().get("detail") as List<Object>

        "detil" /= detail.map {
            json(fromObject(it)) {
                "nama" *= "$.name"
            }
        }.toList()

        - "detail"
       
    }.getPrettyString()

    print(value)
}

Output:

{
  "detil" : [ {
    "nama" : "huda"
  }, {
    "nama" : "syahnaz"
  } ]
}

Hope it helps.

The main idea i…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by hudavianto92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants