可以使用JSONATA来排除特定字段吗?

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

Is it possible to exclude certain fields using JSONATA?

问题

使用JSONATA,是否可以在不使用对象构建的情况下排除深层结构中的某些字段?例如,对于以下对象:

{
	"collection": [
		{
			"id": "ABC",
			"learningunit": {
				"metadata": {
					"show": true,
					"unitType": {
						"code": "U",
						"value": "Unit"
					}
				}
			}
		},
		{
			"id": "UYE",
			"learningunit": {
				"metadata": {
					"show": false,
					"unitType": {
						"code": "C",
						"value": "COURSE"
					}
				}
			}
		}
	]
}

我们是否可以排除字段 "show" 和 "value" 以获得以下结果:

{
	"collection": [
		{
			"id": "ABC",
			"learningunit": {
				"metadata": {
					"unitType": {
						"code": "U"
					}
				}
			}
		},
		{
			"id": "UYE",
			"learningunit": {
				"metadata": {
					"unitType": {
						"code": "C"
					}
				}
			}
		}
	]
}

请注意,以下对象构建表达式可以完成此任务,但如果对象复杂,编写起来可能会很繁琐:

{
	"collection": collection.{
		"id": id,
		"learningunit": learningunit.{
			"metadata": metadata.{
				"unitType": unitType.{
					"code": code
				}
			}
		}
	}
}

希望这有所帮助。

英文:

Using JSONATA, is it possible to exclude certain fields that are nested in a deep structure without using object construction? For example, with the following object

{
	"collection": [
		{
			"id": "ABC",			
			"learningunit": {
				"metadata": {
					"show": true,
					"unitType": {
						"code": "U",						
						"value": "Unit"
					}					
				}
			}
		},
		{
			"id": "UYE",			
			"learningunit": {
				"metadata": {
					"show": false,
					"unitType": {
						"code": "C",						
						"value": "COURSE"
					}					
				}
			}
		}
	]
}

can we exclude the field "show" and "value" in order to get the following result.

{
	"collection": [
		{
			"id": "ABC",			
			"learningunit": {
				"metadata": {					
					"unitType": {
						"code": "U"						
					}					
				}
			}
		},
		{
			"id": "UYE",			
			"learningunit": {
				"metadata": {					
					"unitType": {
						"code": "C"						
					}					
				}
			}
		}
	]
}

FYI, the following object construction expression does the job but it is cumbersome to write if the object is complex.

{"collection":collection.
    {
    "id": id,
    "learningunit": learningunit.
        { 
            "metadata": metadata. 
            {
                "unitType": unitType.
                {
                    "code": code
                }
            }
        }
    }
} 

答案1

得分: 0

您可以使用transform操作符并从嵌套结构中删除所有的'值'和'显示'字段:

$$ ~> | ** | {}, ['show', 'value'] |

在live Stedi playground上查看示例:https://stedi.link/Usc1tpg

请注意,如果您只需要在特定路径上清除这些字段,也可以更精确地执行:

$$
  ~> | *.learningunit.metadata | {}, ['show'] |
  ~> | *.learningunit.metadata.unitType | {}, ['value'] |

Playground: https://stedi.link/Bh8cUiM

英文:

You can make use of the transform operator and remove all 'value' and 'show' fields from the nested structure:

$$ ~> | ** | {}, ['show', 'value'] |

See it on the live Stedi playground: https://stedi.link/Usc1tpg

Note that if you need to clear those on a specific path only, you can also do it more surgically:

$$
  ~> | *.learningunit.metadata | {}, ['show'] |
  ~> | *.learningunit.metadata.unitType | {}, ['value'] |

Playground: https://stedi.link/Bh8cUiM

huangapple
  • 本文由 发表于 2023年6月2日 15:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387879.html
匿名

发表评论

匿名网友

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

确定