需要什么样的颠簸规格才能获得所需的输出

What jolt spec will be required to get the desired output

我有一个 JSON 输入数据,其中 authorpublisher 标签只有 ID,它们的完整信息在下面给出关系 标签。我需要将这些 ID 替换为输出中的实际数据 JSON。

通过JOLT转换是否可行?

我尝试了很多组合并阅读了他们的 Javadoc,但到目前为止运气不佳。我可以在 java 代码和颠簸转换中稍作调整,但只想通过 JOLT 转换来完成,这样就不需要创建新的 jar 文件。

我有一个示例输入 JSON 如下所示

  "meta": {
    "total": 3
  },
  "data": [
    {
      "id": "1112245810",
      "title": "Introduction to JavaScript Object Notation",
      "author": "54256",
      "publisher": "57756",
      "edition": "first",
      "published": "2012"
    },
    {
      "id": "1156464683",
      "title": "JSON at Work",
      "author": "15467",
      "publisher": "57756",
      "edition": "second",
      "published": "2014"
    },
    {
      "id": "1004467968",
      "title": "A Tiny Bit Mortal",
      "author": "54256",
      "publisher": "56465",
      "edition": "first",
      "published": "2018"
    }
  ],
  "relationships": [
    {
      "id": "54256",
      "type": "author",
      "attributes": {
        "first-name": "Lindsay",
        "last-name": "Bassett",
        "city": "Michigan"
      }
    },
    {
      "id": "15467",
      "type": "author",
      "attributes": {
        "first-name": "Tom",
        "last-name": "Marrs",
        "city": "Cologne"
      }
    },
    {
      "id": "57756",
      "type": "publisher",
      "attributes": {
        "name": "O'REILLY",
        "established": "1984"
      }
    },
     {
      "id": "56465",
      "type": "publisher",
      "attributes": {
        "name": "APRESS",
        "established": "1979"
      }
    }
    ]
}

和所需的输出 JSON 需要

{
  "meta": {
    "total": 3
  },
  "data": [
    {
      "id": "1112245810",
      "title": "Introduction to JavaScript Object Notation",
      "author": {
        "first-name": "Lindsay",
        "last-name": "Bassett",
        "city": "Michigan"
      },
      "publisher": {
        "name": "O'REILLY",
        "established": "1984"
      },
      "edition": "first",
      "published": "2012"
    },
    {
      "id": "1156464683",
      "title": "JSON at Work",
      "author": {
        "first-name": "Tom",
        "last-name": "Marrs",
        "city": "Cologne"
      },
      "publisher": {
        "name": "O'REILLY",
        "established": "1984"
      },
      "edition": "second",
      "published": "2014"
    },
    {
      "id": "1004467968",
      "title": "A Tiny Bit Mortal",
      "author": {
        "first-name": "Lindsay",
        "last-name": "Bassett",
        "city": "Michigan"
      },
      "publisher": {
        "name": "APRESS",
        "established": "1979"
      },
      "edition": "first",
      "published": "2018"
    }
  ],
}

这会产生预期的结果(并且可能会进一步重构)。

围绕它的主要想法是将 JSON 设置为键控,然后它可以 "joined" 为键控。请参阅下面每个班次的输出和简要说明。

完整规格

[
  {
    //shift 1
    "operation": "shift",
    "spec": {
      "*": "&",
      "relationships": {
        "*": {
          "type": {
            "*": {
              "@(2,attributes)": "@2.@(3,id)"
            }
          }
        }
      }
    }
  },
  {
    //shift 2
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "author": {
            "*": {
              "@(2)": "data.@2.data.[]"
            }
          }
        }
      }
    }
  },
  {
    //shift 3
    "operation": "shift",
    "spec": {
      "*": "&",
      "author": {
        "*": {
          "@": "data.&.author"
        }
      }
    }
  },
  {
    //shift 4
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "data": {
            "*": {
              "@(id)": "data.@(id).id",
              "@(title)": "data.@(id).title",
              "@(2,author)": "data.@(id).author",
              "@(publisher)": "data.@(id).publisher",
              "@(edition)": "data.@(id).edition",
              "@(published)": "data.@(id).published"
            }
          }
        }
      }
    }
  },
  {
    //shift 5
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "publisher": {
            "*": {
              "@(2)": "data.@2.data.[]"
            }
          }
        }
      }
    }
  },
  {
    //shift 6
    "operation": "shift",
    "spec": {
      "*": "&",
      "publisher": {
        "*": {
          "@": "data.&.publisher"
        }
      }
    }
  },
  {
    //shift 7
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "data": {
            "*": {
              "@(id)": "data.@(id).id",
              "@(title)": "data.@(id).title",
              "@(author)": "data.@(id).author",
              "@(2,publisher)": "data.@(id).publisher",
              "@(edition)": "data.@(id).edition",
              "@(published)": "data.@(id).published"
            }
          }
        }
      }
    }
  },
  {
    //shift 8
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "@": "data.[]"
        }
      }
    }
  }
]

班次 1

由各自的 ID 键入的关系:

  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "relationships": {
        "*": {
          "type": {
            "*": {
              "@(2,attributes)": "@2.@(3,id)"
            }
          }
        }
      }
    }
  }
// ...
  "author" : {
    "54256" : {
      "first-name" : "Lindsay",
      "last-name" : "Bassett",
      "city" : "Michigan"
    },
    "15467" : {
      "first-name" : "Tom",
      "last-name" : "Marrs",
      "city" : "Cologne"
    }
  },
  "publisher" : {
    "57756" : {
      "name" : "O'REILLY",
      "established" : "1984"
    },
    "56465" : {
      "name" : "APRESS",
      "established" : "1979"
    }
  }
}

班次 2 和班次 5

由作者 (2) 或出版商 (5) 各自的 ID 键入的数据:

  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "author": {
        //"publisher": {
            "*": {
              "@(2)": "data.@2.data.[]"
            }
          }
        }
      }
    }
  }
  "data" : {
    "54256" : {
      "data" : [ {
        "id" : "1112245810",
        "title" : "Introduction to JavaScript Object Notation",
        "author" : "54256",
        "publisher" : "57756",
        "edition" : "first",
        "published" : "2012"
      }, {
        "id" : "1004467968",
        "title" : "A Tiny Bit Mortal",
        "author" : "54256",
        "publisher" : "56465",
        "edition" : "first",
        "published" : "2018"
      } ]
    },
    "15467" : {
      "data" : [ {
        "id" : "1156464683",
        "title" : "JSON at Work",
        "author" : "15467",
        "publisher" : "57756",
        "edition" : "second",
        "published" : "2014"
      } ]
    }
  },

班次 3 和班次 6

现在将作者 (3) 或出版商 (6) 移至数据旁边:

  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "author": {
    //"publisher": {
        "*": {
          "@": "data.&.author"
        //"@": "data.&.publisher"
        }
      }
    }
  },
   "54256" : {
      "data" : [ {
        "id" : "1112245810",
        "title" : "Introduction to JavaScript Object Notation",
        "author" : "54256",
        "publisher" : "57756",
        "edition" : "first",
        "published" : "2012"
      }, {
        "id" : "1004467968",
        "title" : "A Tiny Bit Mortal",
        "author" : "54256",
        "publisher" : "56465",
        "edition" : "first",
        "published" : "2018"
      } ],
      "author" : {
        "first-name" : "Lindsay",
        "last-name" : "Bassett",
        "city" : "Michigan"
      }
    },
    "15467" : {

规范 4 和 7

将作者 (4) 或发布者 (7) 复制到每个数据元素并按数据 ID 键:

  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "data": {
            "*": {
              "@(id)": "data.@(id).id",
              "@(title)": "data.@(id).title",
              "@(2,author)": "data.@(id).author",
            //"@(author)": "data.@(id).author",
              "@(publisher)": "data.@(id).publisher",
            //"@(2,publisher)": "data.@(id).publisher",
              "@(edition)": "data.@(id).edition",
              "@(published)": "data.@(id).published"
            }
          }
        }
      }
    }
  },
"data" : {
    "1112245810" : {
      "id" : "1112245810",
      "title" : "Introduction to JavaScript Object Notation",
      "author" : {
        "first-name" : "Lindsay",
        "last-name" : "Bassett",
        "city" : "Michigan"
      },
      "publisher" : "57756",
      "edition" : "first",
      "published" : "2012"
    },
    "1004467968" : {
      "id" : "1004467968",
      "title" : "A Tiny Bit Mortal",
      "author" : {
        "first-name" : "Lindsay",
        "last-name" : "Bassett",
        "city" : "Michigan"
      },
      "publisher" : "56465",
      "edition" : "first",
      "published" : "2018"
    },

规格 8

删除数据 id 作为键:

  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "@": "data.[]"
        }
      }
    }
  }
  "data" : [ {
    "id" : "1112245810",
    "title" : "Introduction to JavaScript Object Notation",
    "author" : {
      "first-name" : "Lindsay",
      "last-name" : "Bassett",
      "city" : "Michigan"
    },
    "publisher" : {
      "name" : "O'REILLY",
      "established" : "1984"
    },
    "edition" : "first",
    "published" : "2012"
  }, {
    "id" : "1156464683",