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

json - Fetch non-array objects in collection with certain value - Stack Overflow

programmeradmin2浏览0评论

Given a collection (not an array) like this:

{
  "field1": {
    "type": "string",
    "value": "test"
  },
  "field2": {
    "type": "array",
    "value": []
  },
  "field3": {
    "type": "string",
    "value": "test"
  },
  "field4": {
    "type": "array",
    "value": []
  }
}

how do I end up with just (filtering on type="array"):

{
  "field2": {
    "type": "array",
    "value": []
  },
  "field4": {
    "type": "array",
    "value": []
  }
}

Given a collection (not an array) like this:

{
  "field1": {
    "type": "string",
    "value": "test"
  },
  "field2": {
    "type": "array",
    "value": []
  },
  "field3": {
    "type": "string",
    "value": "test"
  },
  "field4": {
    "type": "array",
    "value": []
  }
}

how do I end up with just (filtering on type="array"):

{
  "field2": {
    "type": "array",
    "value": []
  },
  "field4": {
    "type": "array",
    "value": []
  }
}
Share Improve this question edited Mar 24 at 17:25 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 24 at 17:23 L SzijL Szij 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The JSONata wildcard operator will select the values of all fields in the context object.

Option 1

$map($spread($), function($x){
  $x.*.type = 'array' ? {
    $keys($x): $x.*
  }
})

JSONata Playground: https://jsonatastudio/playground/f5648325

Option 2

$.*[type="array"]

The output will look like:

[
  {
    "type": "array",
    "value": []
  },
  {
    "type": "array",
    "value": []
  }
]

JSONata Playground: https://jsonatastudio/playground/3007924a

发布评论

评论列表(0)

  1. 暂无评论