【问题标题】:How to normalize this recursive nested JSON如何规范化这个递归嵌套 JSON
【发布时间】:2018-04-03 06:17:21
【问题描述】:

尝试使用 Normalizr 规范化包含与父级相同类型的嵌套模式的我的有效负载时遇到一点问题

例如

{
  id: 123,
  sections:{
    section: [{
      id: 1,
      name: "test",
      sections: {
        section: {
          id: 125,
          name: "test125"
        }
      }
    }, {
      id: 2,
      name: "test2"
      sections: {
        section: [
          {
            id: 124,
            name: "test124"
          }
        ]
      }
    }, {
      id: 3,
      name: "test3"
    }]
  } 
}

在上面的json结构中,嵌套部分可以是一个对象,也可以是一个数组。

【问题讨论】:

    标签: normalization normalize normalizr


    【解决方案1】:

    嵌套 cmets 也有类似的问题。以下是我的解决方法:

    export const comment = new schema.Entity("comment", {}, {
        idAttribute: "key"
    })
    comment.define({
        user: user,
        reactions: {
            data: [reaction]
        },
        children: [comment]
    })
    
    
    export const post = new schema.Entity("post", {
        user: user,
        files: [image],
        comments: {
            data: [comment]
        },
        reactions: {
            data: [reaction]
        }
    }, {
        idAttribute: "key"
    })
    

    基本上,我先将comment 定义为一个新实体,然后再将其用于自己的实体定义中。我像往常一样在构造函数中定义的其他模式(参见post 模式以获取示例)。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这是一个 jq 过滤器,它将规范化数据:

      def enumerate:
        if type=="array" then .[] else . end ;
      def sectionids:
          [ .sections.section | enumerate | .id // empty | tostring ]
        | if .==[] then {} else {sections:.} end ;
      def sections:
          {sections:{section:[.]}}
        | .. | .section? | enumerate | objects | del(.sections) + sectionids ;
      
      {
        "result": .id,
        "entities": {
          "sections": (reduce sections as $s ({};.["\($s.id)"]=$s))
        }
      }
      

      示例运行(假设 data.json 中的正确 json 数据和 filter.jq 中的上述过滤器)

      $ jq -M -f filter.jq data.json
      {
        "result": 123,
        "entities": {
          "sections": {
            "123": {
              "id": 123,
              "sections": [
                "1",
                "2",
                "3"
              ]
            },
            "1": {
              "id": 1,
              "name": "test",
              "sections": [
                "125"
              ]
            },
            "2": {
              "id": 2,
              "name": "test2",
              "sections": [
                "124"
              ]
            },
            "3": {
              "id": 3,
              "name": "test3"
            },
            "125": {
              "id": 125,
              "name": "test125"
            },
            "124": {
              "id": 124,
              "name": "test124"
            }
          }
        }
      }
      

      Try it online!

      【讨论】:

        猜你喜欢
        • 2017-02-08
        • 2021-07-23
        • 2017-10-27
        • 2020-11-09
        • 1970-01-01
        • 2022-06-21
        • 1970-01-01
        • 2021-05-19
        • 2021-11-22
        相关资源
        最近更新 更多