最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Return root empty array from Jolt instead of null - Stack Overflow

programmeradmin2浏览0评论

I have the following input:

{
  "data": [
    {
      "ID": 1,
      "TYPE": "A"
    }
  ]
}

And the following Jolt transform:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "ID": "[&1].id",
          "TYPE": "[&1].type"
        }
      }
    }
  }
]

It works fine, and returns:

[
  {
    "id": 1,
    "type": "A"
  }
]

However when provided:

{     
  "data": [] 
}

It returns null but I want it to return an empty array, like so:

[]

Is there any way to achieve this?

I have the following input:

{
  "data": [
    {
      "ID": 1,
      "TYPE": "A"
    }
  ]
}

And the following Jolt transform:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "ID": "[&1].id",
          "TYPE": "[&1].type"
        }
      }
    }
  }
]

It works fine, and returns:

[
  {
    "id": 1,
    "type": "A"
  }
]

However when provided:

{     
  "data": [] 
}

It returns null but I want it to return an empty array, like so:

[]

Is there any way to achieve this?

Share Improve this question edited Nov 20, 2024 at 20:47 Barbaros Özhan 65.4k11 gold badges36 silver badges61 bronze badges asked Nov 20, 2024 at 20:31 Illay Ben NounIllay Ben Noun 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If it would suffice to get

[
  {
    "ID": 1,
    "TYPE": "A"
  }
]

then the following single spec would handle your problem :

[
  {
    "operation": "shift",
    "spec": {
      "data": ""
    }
  }
]

but for your case you can convert it to the following :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "id": "=(@(1,ID))",
          "type": "=(@(1,TYPE))"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "ID|TYPE": ""
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": ""
    }
  }
]

third operation of which is the identical with the previously suggested one.

发布评论

评论列表(0)

  1. 暂无评论