Json Jolt-将数据转移到多个子数组

时间:2018-08-08 16:01:09

标签: json jolt

我正在尝试进行一些轮班操作,以将一些数据复制到多个阵列中,但是我无法将数据正确地放置到阵列中。

这就是我所拥有的

{
  "input": {
    "rating_date": "2018-08-06",
    "Rates": {
      "name": "Generic ratings Card",
      "version": "v1",
      "value": "2600.00",
      "name1": [
        {
          "hits": "321",
          "genre": "fiction",
          "ratingName": "name1"
        },
        {
          "hits": "654",
          "genre": "fiction",
          "ratingName": "name1"
        }
      ],
      "name2": [
        {
          "hits": "123",
          "genre": "nonfiction",
          "ratingName": "name2"
        },
        {
          "hits": "456",
          "genre": "fiction",
          "ratingName": "name2"
        }
      ]
    }
  },
  "spec": {
    "operation": "shift",
    "spec": {
      "rating_date": "rating_by_date[#0].date",
      "Rates": {
        "name*": {
          "$": "rating_by_date[#2].ratecards[#1].type",
          "*": {
            "@": "rating_by_date[#3].ratecards[#2].rates[#0].&"
          }
        }
      }
    }
  },
  "expected": {
    "rating_by_date": [
      {
        "date": "2018-08-06",
        "ratecards": [
          {
            "type": "name1",
            "rates": [
              {
                "hits": "321",
                "genre": "fiction",
                "ratingName": "name1"
              },
              {
                "hits": "654",
                "genre": "fiction",
                "ratingName": "name1"
              }
            ]
          },
          {
            "type": "name2",
            "rates": [
              {
                "hits": "123",
                "genre": "nonfiction",
                "ratingName": "name2"
              },
              {
                "hits": "456",
                "genre": "fiction",
                "ratingName": "name2"
              }
            ]
          }
        ]
      }
    ]
  }
}

这个想法是将对象放入rates数组中,并将它们转换(在以后的转换中)为键值对,我已经知道该怎么做。

现在,我的规范没有将数据转移到适当的数组中,而我最终得到了这样的东西:

{
  "rating_by_date" : [ {
    "date" : "2018-08-06",
    "ratecards" : [ {
      "type" : "name1",
      "rates" : [ {
        "0" : {
          "hits" : "321",
          "genre" : "fiction",
          "ratingName" : "name1"
        }
      } ]
    }, {
      "rates" : [ {
        "1" : {
          "hits" : "654",
          "genre" : "fiction",
          "ratingName" : "name1"
        }
      } ]
    } ]
  }, {
    "ratecards" : [ {
      "type" : "name2",
      "rates" : [ {
        "0" : {
          "hits" : "123",
          "genre" : "nonfiction",
          "ratingName" : "name2"
        }
      } ]
    }, {
      "rates" : [ {
        "1" : {
          "hits" : "456",
          "genre" : "fiction",
          "ratingName" : "name2"
        }
      } ]
    } ]
  } ]
}

不是在同一数组中创建新对象,而是一起创建新数组。我显然不完全了解如何在RHS上引用数组结构,我希望得到澄清。

1 个答案:

答案 0 :(得分:0)

我设法使其正常工作,但我不确定为什么一步一步都无法工作。这是我要做的工作:

[
  {
    "operation": "shift",
    "spec": {
      "rating_date": "rating_by_date[0].date",
      "Rates": {
        "name*": {
          "$": "rating_by_date[0].ratecards.&.type",
          "@": "rating_by_date[0].ratecards.&.rates"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "rating_by_date": {
        "*": {
          "ratecards": {
            "*": {
              "@": "rating_by_date[&3].ratecards2[]"
            }
          },
          "*": "rating_by_date[&1].&"
        }
      }
    }
  }
]