Replies: 3 comments 12 replies
-
In go there is no way to rename a map key. You can, however, add and delete keys so deleting a key with |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer : I'm not talking about Go here but about the possibilities offered by your library. I'm receiving a deep JSON object inside one of my endpoint and I want to rename some specific keys. It's very easy to change object values using your library but I'm struggling to change object keys. |
Beta Was this translation helpful? Give feedback.
-
func TestRename(t *testing.T) {
data := oj.MustParse([]byte(`
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192",
"other": {
"type": 456
}
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
`))
path := jp.MustParseString("$..type")
fmt.Printf("before: %s\n", pretty.SEN(data))
for _, np := range path.Locate(data, -1) {
value := np.First(data)
np.MustDel(data)
np[len(np)-1] = jp.Child("newType")
np.MustSetOne(data, value)
}
fmt.Printf("after: %s\n", pretty.SEN(data))
} |
Beta Was this translation helpful? Give feedback.
-
Hi after looking at the documentation I can't find a way to rename JSON object keys. Can you show me how to do that please.
Best regards
Beta Was this translation helpful? Give feedback.
All reactions