如何更改json对象数组的字段名称

So I have a project with lots of incoming data about 15 sources in total, of course there are inconsistencies in how each label there data available in their rest api's. I need to Change some of their field names to be consistent with the others, but I am at a loss on how to do this when the data sources are json object arrays. A working example of what I am trying to do is found here playground and below

however I seem to lack the knowledge as to how to make this work when the data is not a single json object , but instead and array of objects that I am unmarshaling.

Another approach is using Maps like in this example but the result is the same, works great as is for single objects, but I can not seem to get it to work with json object arrays. Iteration through arrays is not a possibility as I am collecting about 8,000 records every few minutes.

package main

import (
    "encoding/json"
    "os"
)

type omit bool

type Value interface{}

type CacheItem struct {
    Key    string `json:"key"`
    MaxAge int    `json:"cacheAge"`
    Value  Value  `json:"cacheValue"`
}

func NewCacheItem() (*CacheItem, error) {
    i := &CacheItem{}
    return i, json.Unmarshal([]byte(`{
      "key": "foo",
      "cacheAge": 1234,
      "cacheValue": {
        "nested": true
      }
    }`), i)
}

func main() {
    item, _ := NewCacheItem()

    json.NewEncoder(os.Stdout).Encode(struct {
        *CacheItem

        // Omit bad keys
        OmitMaxAge omit `json:"cacheAge,omitempty"`
        OmitValue  omit `json:"cacheValue,omitempty"`

        // Add nice keys
        MaxAge int    `json:"max_age"`
        Value  *Value `json:"value"`
    }{
        CacheItem: item,

        // Set the int by value:
        MaxAge: item.MaxAge,

        // Set the nested struct by reference, avoid making a copy:
        Value: &item.Value,
    })
}
dongying6179
dongying6179 因此,您的最终目标是输出标准化的JSON,对吗?也就是说,您没有将JSON用作执行Go逻辑的中间步骤,而是将Go用作了从JSON到JSON的中间步骤-是吗?
2 年多之前 回复
doutongxuan1614
doutongxuan1614 听起来您好像有一个解决方案,但是您担心它的性能,但是实际上尚未测试它的性能。我经常听到这样的建议,即性能问题很难准确预测,因此只需使其正常工作,然后再担心性能问题就担心性能...
2 年多之前 回复
doufan6033
doufan6033 我想我有您的问题,但是我仍然认为,如果您添加了一些您希望能够处理的示例数组输入,并且如果可能的话,您尝试使用一些代码来处理它,将更容易理解。即使它已损坏或未完成或未按照您想要的方式运行。
2 年多之前 回复
doufu5747
doufu5747 好吧,它比重复执行x15go例程时要遍历要复杂得多,每5分钟大约需要8k的大容量插入
2 年多之前 回复
duan48961411
duan48961411 我认为您想要的是不可能的。遍历所有项似乎是唯一的选择,您在解编组期间已经在这样做了(虽然是隐式的),从X到2X的效果还不错,每分钟8k的记录也不是很多,我担心如果是每秒。
2 年多之前 回复

1个回答

It appears your desired output is JSON. You can accomplish the conversion by unmarshaling into a slice of structs, and then iterating through each of those to convert them to the second struct type (your anonymous struct above), append them into a slice and then marshal the slice back to JSON:

package main

import (
    "fmt"
    "encoding/json"
)

type omit bool

type Value interface{}

type CacheItem struct {
    Key    string `json:"key"`
    MaxAge int    `json:"cacheAge"`
    Value  Value  `json:"cacheValue"`
}

type OutGoing struct {
    // Omit bad keys
    OmitMaxAge omit `json:"cacheAge,omitempty"`
    OmitValue  omit `json:"cacheValue,omitempty"`

    // Add nice keys
    Key    string `json:"key"`
    MaxAge int    `json:"max_age"`
    Value  *Value `json:"value"`
}

func main() {
    objects := make([]CacheItem, 0)
    sample := []byte(`[
    {
      "key": "foo",
      "cacheAge": 1234,
      "cacheValue": {
        "nested": true
      }},
    {
      "key": "baz",
      "cacheAge": 123,
      "cacheValue": {
        "nested": true
    }}]`)

    json.Unmarshal(sample, &objects)

    out := make([]OutGoing, 0, len(objects))
    for _, o := range objects {
        out = append(out, OutGoing{Key:o.Key, MaxAge:o.MaxAge, Value:&o.Value})
    }
    s, _ := json.Marshal(out)
    fmt.Println(string(s))
}

This outputs

[{"key":"foo","max_age":1234,"value":{"nested":true}},{"key":"baz","max_age":123,"value":{"nested":true}}]

You could probably skip this iteration and conversion code if you wrote custom MarshalJSON and UnmarshalJSON methods for your CacheItem type, instead of relying on struct field tags. Then you could pass the same slice to both Unmarshal and Marshal.

To me there's no obvious performance mistake with these approaches -- contrast with building a string in a loop using the + operator -- and when that's the case it's often best to just get the software to work and then test for performance rather than ruling out a solution based on fears of performance issues without actually testing.

If there's a performance problem with the above approaches, and you really want to avoid marshal and unmarshal completely, you could look into byte replacement in the JSON data (e.g. regexp). I'm not recommending this approach, but if your changes are very simple and the inputs are very consistent it could work, and it would give another approach you could performance test, and then you could compare performance test results.

dongshi1869
dongshi1869 您会得到支票,因为这正是我处理其他规范化问题并准备带有时间戳的批量插入的方式。 但是正如预期的那样(缺少一些简单的东西),我从来没有想到要追加到第二个结构。 在等待某人弄清楚我是多么愚蠢的同时,我想出了一种肮脏的骇客方法,但我认为您的解决方案是应对这种情况的明显方法。 感谢您的时间和帮助,很抱歉,我没有在上面首先回答您的评论,正在睡觉:-)
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐