Skip to content

how to rename JSON object keys #172

Answered by ohler55
nsitbon asked this question in Q&A
May 7, 2024 · 3 comments · 12 replies
Discussion options

You must be logged in to vote
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

Replies: 3 comments 12 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
12 replies
@ohler55
Comment options

@nsitbon
Comment options

@ohler55
Comment options

@nsitbon
Comment options

@ohler55
Comment options

Comment options

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