如何将值从上层对象移动到下层对象,并在颠簸转换中保持分组

时间:2019-02-04 19:10:06

标签: jolt

我正在尝试从大型json文件中提取数据。我需要从提取的嵌套对象和列表中匹配数据。

我为此编写了许多规范。最接近的“错误”解决方案使输出充满空值。当前的规格非常接近,但是它将一些数据保留在列表中,我需要以不同的方式分配它们。它使用两班制。

    [
      {
        "Contents": [
          {
            "original": "<h4>Hour 1</h4>",
            "type": "other"
          },
          {
            "content": {
              "artist": "01art1816-01",
              "catalog_no": "cat1816-01",
              "comp_work": "comp1816-01",
              "organ": "org1816-01"
            },
            "original": "1816-01",
            "type": "listing"
          },
          {
            "content": {
              "artist": "art1816-02",
              "catalog_no": "1816-02",
              "comp_work": "1816-02",
              "organ": "1816-02"
            },
            "original": "1816-02",
            "type": "listing"
          }
        ],
        "filepath": "/listings/2018/1816/index.html",
        "program_number": "1816"
      },
      {
        "Contents": [
          {
            "original": "<h4>Hour 1</h4>",
            "type": "other"
          },
          {
            "content": {
              "artist": "02art1839-01",
              "catalog_no": "1839-01",
              "comp_work": "1839-01",
              "organ": "1839-01"
            },
            "original": "1839-01",
            "type": "listing"
          },
          {
            "original": "origin-othr",
            "type": "other"
          }
        ],
        "filepath": "/listings/2018/1839/index.html",
        "program_number": "1839"
      },
      {
        "Contents": [
          {
            "original": "<h4>Part 1</h4>",
            "type": "other"
          },
          {
            "content": {
              "artist": "03art8843-01",
              "catalog_no": "8843-01",
              "comp_work": "8843-01",
              "organ": "8843-01"
            },
            "original": "8843-01",
            "type": "listing"
          },
          {
            "content": {
              "artist": "art8843-02",
              "catalog_no": "8843-02",
              "comp_work": "8843-02",
              "organ": "8843-02"
            },
            "original": "8843-02",
            "type": "listing"
          }
        ],
        "filepath": "/listings/1988/8843/index.html",
        "program_number": "8843"
      }
    ]

我需要“ program_number”和“ filepath”位于内容下的每个对象中。

预期:

    {
      "playlist": [{
        "show": "1816",
        "path": "/listings/2018/1816/index.html",
        "artist": "artist1816-01",
        "catalog_no": "cat1816-01",
        "comp_work": "comp1816-01",
        "organ": "org1816-01"
      }, {
        "show": "1816",
        "path": "/listings/2018/1816/index.html",
        "artist": "artist1816-02",
        "catalog_no": "cat1816-02",
        "comp_work": "comp1816-02",
        "organ": "org1816-02"
      }, {
        "show": "1839",
        "path": "/listings/2018/1839/index.html",
        "artist": "artist1839-01",
        "catalog_no": "cat1839-01",
        "comp_work": "comp1839-01",
        "organ": "org1839-01"
      }, {
        "show": "8843",
        "path": "/listings/1988/8843/index.html",
        "artist": "artist8843-01",
        "catalog_no": "cat8843-01",
        "comp_work": "comp8843-01",
        "organ": "org8843-01"
      }, {
        "show": "8843",
        "path": "/listings/1988/8843/index.html",
        "artist": "artist8843-02",
        "catalog_no": "cat8843-02",
        "comp_work": "comp8843-02",
        "organ": "org8843-02"
      }]
    }

实际:

    {
      "playlist": [{
        "show": "1816",
        "path": "/listings/2018/1816/index.html",
        "artist": ["artist1816-01", "artist1816-02"],
        "catalog_no": ["cat1816-01", "cat1816-02"],
        "comp_work": ["comp1816-01", "comp1816-02"],
        "organ": ["org1816-01", "org1816-02"]
      }, {
        "show": "1839",
        "path": "/listings/2018/1839/index.html",
        "artist": "artist1839-01",
        "catalog_no": "cat1839-01",
        "comp_work": "comp1839-01",
        "organ": "org1839-01"
      }, {
        "show": "8843",
        "path": "/listings/1988/8843/index.html",
        "artist": ["artist8843-01", "artist8843-02"],
        "catalog_no": ["cat8843-01", "cat8843-02"],
        "comp_work": ["comp8843-01", "comp8843-02"],
        "organ": ["org8843-01", "org8843-02"]
      }]
    }

使用此规范:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "program_number": "playlist.[&1].show",
            "filepath": "playlist.[&1].path",
            "Contents": {
              "*": {
                "type": {
                  "listing": {
                    "@(2,content)": "playlist.[&5]"
                  }
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "playlist": {
            "*": {
              "*": {
                "artist": "playlist[&2].artist",
                "catalog_no": "playlist[&2].catalog_no",
                "comp_work": "playlist[&2].comp_work",
                "organ": "playlist[&2].organ",
                "@show": "playlist[&2].show",
                "@path": "playlist[&2].path"
              }
            }
          }
        }
      }
    ]

1 个答案:

答案 0 :(得分:0)

经过一些重做:

移位如下:

  1. 将文件路径和program_number复制到数组
  2. 按类型列表过滤
  3. 格式化
 [
    {
      "operation": "shift",
      "spec": {
        "*": {
          "Contents": {
            "*": {
              "@": "[&3].[&1]",
              "@(2,filepath)": "[&3].[&1].path",
              "@(2,program_number)": "[&3].[&1].show"
            }
          }
        }
      }
    },
    {
      "operation": "shift",
      "spec": {
        "*": {
          "*": {
            "type": {
              "listing": {
                "@(2)": "[]"
              }
            }
          }
        }
      }
    },
    {
      "operation": "shift",
      "spec": {
        "*": {
          "show": "playlist.[&1].&",
          "path": "playlist.[&1].&",
          "content": {
            "*": {
              "@": "playlist.[&3].&"
            }
          }
        }
      }
    }
]