json - yq expression to replace first-level keys with their values? - Stack Overflow

Given input JSON like this:{"a": {"aa": {"aaa1": "value","

Given input JSON like this:

{
    "a": {
        "aa": {
            "aaa1": "value",
            "aaa2": "value"
        }
    },
    "b": {
        "bb": {
            "bbb1": "value",
            "bbb2": "value"
        }
    }
}

I want to use Mike Farah's yq to replace first-level keys with their values, and output:

aa:
  aaa1: value
  aaa2: value
bb:
  bbb1: value
  bbb2: value

Notes:

  • This is just a simplified representative example. Actual structure can be deeper than this. Actual data contains many more first-level keys. Actual key names are more varied; they don't match the a..., b... patterns shown here.
  • One aspect of this example structure does match the actual data, though: each first-level key has only one child.

That is, I want to replace each first-level key with its (one and only) child.

I've spent hours trying to do this with various operators. I'm stumped.

For example, I thought perhaps I could simply iterate over the first-level keys and replace them with their values:

with_entries(.parent |= .value)

or (very similar, just clutching at straws):

with_entries(.parent |= .)

Nope. Neither seems to have any effect. (Their output is the same as their input.)

I'd prefer to not embarrass myself further by describing my other failed attempts.

Given input JSON like this:

{
    "a": {
        "aa": {
            "aaa1": "value",
            "aaa2": "value"
        }
    },
    "b": {
        "bb": {
            "bbb1": "value",
            "bbb2": "value"
        }
    }
}

I want to use Mike Farah's yq to replace first-level keys with their values, and output:

aa:
  aaa1: value
  aaa2: value
bb:
  bbb1: value
  bbb2: value

Notes:

  • This is just a simplified representative example. Actual structure can be deeper than this. Actual data contains many more first-level keys. Actual key names are more varied; they don't match the a..., b... patterns shown here.
  • One aspect of this example structure does match the actual data, though: each first-level key has only one child.

That is, I want to replace each first-level key with its (one and only) child.

I've spent hours trying to do this with various operators. I'm stumped.

For example, I thought perhaps I could simply iterate over the first-level keys and replace them with their values:

with_entries(.parent |= .value)

or (very similar, just clutching at straws):

with_entries(.parent |= .)

Nope. Neither seems to have any effect. (Their output is the same as their input.)

I'd prefer to not embarrass myself further by describing my other failed attempts.

Share Improve this question edited Mar 12 at 1:16 Graham Hannington asked Mar 11 at 10:13 Graham HanningtonGraham Hannington 2,0011 gold badge18 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Note that your JSON input, formatted as YAML, already looks like this:

a:
  aa:
    aaa1: value
    aaa2: value
b:
  bb:
    'bbb1:': value
    'bbb2:': value

Thus, to get a document containing just the second-level content (regardless of the number of the keys contained, btw), simply iterate over the first level using .[], and set the output format to YAML using --output-format yaml or -oy (if needed, depending on the extension of your input file, also set the input format to JSON using --input-format json or -pj):

yq -pj -oy '.[]' input.json 
aa:
  aaa1: value
  aaa2: value
bb:
  'bbb1:': value
  'bbb2:': value

Note that your JSON input sample has colons in the keys bbb1: and bbb2: (maybe just a typo), so they get escaped (quoted) in the output YAML.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744802140a4594556.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信