如何将一个对象添加到另一个对象中并使用jolt规范删除该对象

huangapple go评论54阅读模式
英文:

How to add one object into the another objects and remove that object using jolt spec

问题

[
  {
    "continent": "North America",
    "item": "sugar",
    "price": 4,
    "state": "Alberta",
    "country": "Canada",
    "lang": "95° 00' W",
    "long": "60° 00' N"
  },
  {
    "continent": "North America",
    "item": "sugar",
    "price": 4.5,
    "state": "California",
    "country": "US",
    "lang": "97° 00' W",
    "long": "38° 00' N"
  }
]
英文:

I want to remove all the nested arrays and put values of "location" and "metarecord" in the rest json object and finally remove these object ("location" and "metarecord").

{
  "info": [
    {
      "item": "sugar",
      "price": 4,
      "state": "Alberta",
      "country": "Canada"
    },
    {
      "item": "sugar",
      "price": 4.5,
      "state": "California",
      "country": "US"
    }
  ],
  "location": {
    "continent": [
      {
        "country": "US",
        "coordinates": {
          "lang": "97º 00' W",
          "long": "38º 00' N"
        }
      },
      {
        "country": "Canada",
        "coordinates": {
          "lang": "95° 00' W",
          "long": "60° 00' N"
        }
      }
    ]
  },
  "metarecord": {
    "continent": "North America"
  }
}

the below is what i want to achieve:
remove all the nested arrays and put values of "location" to their appropriate json objects using value of country (e.g if country value of an object is "Canada" then, location of "canada" should be added) and "metarecord" is to all the objects inside "info" as its general

[
  {
    "continent": "North America",
    "item": "sugar",
    "price": 4,
    "state": "Alberta",
    "country": "Canada",
    "lang": "95° 00' W",
    "long": "60° 00' N"
  },
  {
    "continent": "North America",
    "item": "sugar",
    "price": 4.5,
    "state": "California",
    "country": "US",
    "lang": "97º 00' W",
    "long": "38º 00' N"
  }
]

what i tried:

[
  {
    "operation": "shift",
    "spec": {
      "info": {
        "*": {
          "item": "[&1].item",
          "price": "[&1].price",
          "state": "[&1].state",
          "country": "[&1].country"
        }
      },
      "location": {
        "continent": {
          "@": "[&1].continent"
        }
      },
      "metarecord": {
        "continent": {
          "*": {
            "$": "[&2].&1"
          }
        }
      }
    }
  }
]

output: which is not what i want.

[
  {
    "item": "sugar",
    "price": 4,
    "state": "Alberta",
    "country": "Canada"
  },
  {
    "item": "sugar",
    "price": 4.5,
    "state": "California",
    "country": "US"
  }
]

答案1

得分: 1

你可以使用以下的转换操作:

[
  {
    "operation": "shift",
    "spec": {
      "info": {
        "*": {
          "*": "@1,country.&"
        },
        "@2,location.continent": {
          "*": {
            "coordinates": { "*": "@2,country.&" }
          }
        },
        "@2,metarecord.continent": "@1,country.continent"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

在网站 http://jolt-demo.appspot.com/ 上的演示如下:

如何将一个对象添加到另一个对象中并使用jolt规范删除该对象

英文:

You can use the following transformation

[
  { // you can group by the countries while matching the ones taken from different arrays
    "operation": "shift",
    "spec": {
      "info": {
        "*": {
          "*": "@1,country.&",
          "@2,location.continent": { // go two levels up the tree to grab the values
            "*": {
              "coordinates": { "*": "@2,country.&" }
            }
          },
          "@2,metarecord.continent": "@1,country.continent"
        }
      }
    }
  },
  { // pick only one from the repeating components for the arrays
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  },
  { // get rid of the object labels
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is :

如何将一个对象添加到另一个对象中并使用jolt规范删除该对象

huangapple
  • 本文由 发表于 2023年6月19日 14:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定