Get alternative elements in JSON using JQ(输出:JSON 对象)

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

Get alternative elements in JSON using JQ (output: JSON object)

问题

I have translated the provided text. Here it is:

我有一个问题,我收到的JSON数据,根据来源的不同,可能在根级别或另一个元素下有一些数据。

从这个JSON中,我需要提取多个元素到另一个规范化的JSON中,并为值使用自定义键。

示例JSON 1:

{ "name": "John", "last": "Smith", "position": "clerk" }

示例JSON 2:

{ "personData": { "name": "John", "last": "Smith" } }

起初,我使用这个作为我的所有测试JSON都具有第一种形式:

jq '{nombre: .name, apellido: .last, puesto: .position}';

当它们开始为空时,我尝试使用//运算符,但如果我提供键似乎不起作用:

不起作用:

jq '{nombre: .name//.personData.name, apellido: .last, puesto: .position}';

有效(但我不得到任何键):

jq '{nombre: .name//.personData.name, apellido: .last, puesto: .position//null}';

我从未使用过带有条件或逻辑的jq,所以我可能忽略了一些明显的东西。
(实际的JSON文件非常复杂,无论我需要使用哪种逻辑,最终都将应用于十几个字段,所以我正在寻找一个可持续的解决方案。

预期输出将为第一个示例:

{ "nombre" : "John", "apellido": "Smith", "puesto": "clerk" }

对于第二个示例:

{ "nombre" : "John", "apellido": "Smith", "puesto": null }

答案:
最终使用了这种样式,允许在多个块中搜索以提取多个数据:

(.personData // .) + (.addressData // .) | {nameValue: .name, streetValue: .streetName }

(简化版:.name值可以在根级别或.personData.name下,.streetName值可以在根级别或.addressData.streetvalue下,因此上述将两个块的情况规范化为同一个流)

英文:

I have a problem where I'm receiving JSON that depending on the source, may have some data at root level or under another element.

From this JSON I need to extract multiple elements into a nother, normalized JSON, with custom keys for the values.

Sample JSON 1:

{"name":"John","last":"Smith","position": "clerk"}

Sample JSON 2:

{"personData": {"name":"John","last":"Smith"}}

I was using this at the beginning as all my test jsons had the first form:

jq '{nombre: .name, apellido: .last, puesto: .position}'

When they started coming up empty I tries using the // operator but it doesn't seem to work if I'm providing the keys:

Doesn't work:

jq '{nombre: .name//.personData.name, apellido: .last, puesto: .position}'

Works (but I don't get any keys):

jq '{nombre: .name//.personData.name, apellido: .last, puesto: .position//null}'

I've never had to use jq with conditionals or logic like this, so I may be missing something obvious.
(actual json files are pretty extensive and whichever logic I need to use will end up applied to a dozen or so fields, so I'm looking for a sustainable solution.

Expected output would be for the first sample:

{"nombre" : "John", "apellido": "Smith", "puesto": "clerk" }

And for the second sample:

{"nombre" : "John", "apellido": "Smith", "puesto": null}

Answer:
Ended up using this style, which allows for searching in multiple blocks for extracting multiple data:

(.personData // .) + (.addressData // .) | {nameValue: .name, streetValue: .streetName }

(simplified: The .name value can be at root or under .personData.name, and the .streetName value can be at root or under .addressData.streetvalue, so the above normalizes the situation for both blocks into the same stream)

答案1

得分: 3

以下是翻译好的部分:

"nombre: .name, apellido: .last, puesto: .position"
"如果您想要对所有带有 .name 的 JSON 对象进行全面搜索,您可以使用 ..walk/1。如果您只想要一个这样的对象,您可以使用 first/1,这也许是一个好主意,因为它可能会加快速度。"

英文:

One of many possibilities:

(.personData // .) |  {nombre: .name, apellido: .last, puesto: .position}

If you want a fully exhaustive search for all JSON objects with .name, then you could use .. or walk/1. If you want at most one such object, you could use first/1, which might be a good idea anyway as it might speed things up.

答案2

得分: 1

一种方法,使用递归 ..

.. | select(has("name") and has("last"))? |
    { nombre: .name, appelido: .last, puesto: .position }

可在其他上下文中重复使用。

来自 man jq

递归下降:..
递归地下降到 .,生成每个值。这与零参数的递归内置功能相同。这旨在类似于 XPath // 操作符。

英文:

One way, using recursion ..:

.. | select(has("name") and has("last"))? |
    { nombre: .name, appelido: .last, puesto: .position }

Can be re-used in other contexts.

From man jq:

> Recursive Descent: ..
> Recursively descends ., producing every value. This is the same as the zero-argument recurse builtin. This is intended to resemble the XPath // operator.

huangapple
  • 本文由 发表于 2023年5月7日 08:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191802.html
匿名

发表评论

匿名网友

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

确定