how to remove element from JSON array in golang?

huangapple go评论154阅读模式
英文:

how to remove element from JSON array in golang?

问题

我有一个 JSON 字符串数组:

[
  {
    "name":"abc",
    "age":25
  },
  {
    "name":"xyz",
    "age":"26"
  }
]

在运行时,我想从数组中删除 "name"。我们应该如何做到这一点?我不想进行解组操作。

[
  {
    "age":25
  },
  {
    "age":"26"
  }
]
英文:

I have a array of JSON string

ex:

[
 {
  "name":"abc"
  "age":25
 }
 {
  "name":"xyz"
  "age":"26"
 }
]

In run time I want to remove "name" from array . How can we do it . I don't want to unmarshal it .

[
     {
      "age":25
     }
     {
      "age":"26"
     }
  ]

答案1

得分: 2

你可以使用以下包gjsonsjson来实现你想要的功能。

示例代码:

package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/tidwall/gjson"
	"github.com/tidwall/sjson"
)

func main() {
	bJSON := []byte(`       
            [
                {
                    "name": "abc",
                    "age": 25
                },
                {
                    "name": "xyz",
                    "age": 26
                }
            ]
    `)

	newJSON := bJSON
	var err error

	result := gjson.GetBytes(bJSON, "#.age")

	for i := range result.Array() {
		newJSON, err = sjson.DeleteBytes(newJSON, strconv.Itoa(i)+".age")
		if err != nil {
			log.Println(err)
		}
	}

	fmt.Println(string(newJSON))
}

输出结果:

[
  {
    "name": "abc"
  },
  {
    "name": "xyz"
  }
]
英文:

you can do what you want by using, this packages gjson, and sjson.

example:

package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/tidwall/gjson"
	"github.com/tidwall/sjson"
)

func main() {
	bJSON := []byte(`       
            [
                {
                    "name": "abc",
                    "age": 25
                },
                {
                    "name": "xyz",
                    "age": 26
                }
            ]
    `)

	newJSON := bJSON
	var err error

	result := gjson.GetBytes(bJSON, "#.age")

	for i := range result.Array() {
		newJSON, err = sjson.DeleteBytes(newJSON, strconv.Itoa(i)+".age")
		if err != nil {
			log.Println(err)
		}
	}

	fmt.Println(string(newJSON))
}

output:

[
  {
    "name": "abc"
  },
  {
    "name": "xyz"
  }
]

答案2

得分: 1

如果你真的不想解组它,那么你只能使用某种正则表达式替换或构建状态机(实际上是自己解组它)。如果你正在问这个问题,那么你不应该采取这种方法。这种方法只适用于非常高级的用户,并且在非常极端的情况下除外,没有任何优势。

我猜你只是不想费力去编写解组代码,并且不介意使用一个可以为你完成工作的工具,即使这涉及在内部进行解组。

如果我的猜测是正确的,你可以考虑使用像gabs这样的库(免责声明:我从未使用过这个库),它提供了简单的JSON操作。但当然,它在过程中会解组JSON,然后在你要求结果时重新组合它。

英文:

If you literally don't want to unmarshal it, you'll be stuck doing some sort of regex replacement, or building a state machine (effectively unmarshaling it yourself). If you're asking the question, then you shouldn't take this approach. This approach is for very advanced users only, and has no advantages except in very extremely rare situations.

I assume you just don't want to bother with the effort of writing code to unmarshal it, though, and wouldn't mind a tool that does the work for you, even if that involves unmarshaling under the covers.

If I'm right in my guess, you could consider a library like gabs (disclaimer: I've never used this library), which provides for easy JSON manipulation. But of course it unmarshals the JSON in the process, then remarshals it when you ask for the result.

huangapple
  • 本文由 发表于 2017年7月15日 02:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/45109397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定