Transforming the name of key deeper in the JSON structure with jq

后端 未结 3 1355
野性不改
野性不改 2021-01-16 09:46

I have following json:

{
  \"vertices\": [
    {
      \"__cp\": \"foo\",
      \"__type\": \"metric\",
      \"__eid\": \"foobar\",
      \"name\": \"Undert         


        
3条回答
  •  迷失自我
    2021-01-16 10:37

    From your example data it looks like you intend lots of little manipulations so I'd break things out into stages like this:

      .nodes = .vertices                     # \ first take care of renaming
    | del(.vertices)                         # / .vertices to .nodes
    
    | .nodes = [ 
           .nodes[]                          # \ then scan each node
         | . as $n                           # /
    
         | del(._type, .__eid)               # \ whatever key-specific tweaks like 
         | .label = "metric: \(.name)"       # / calculating .label you want can go here
    
         | reduce keys[] as $k (             # \
             {};                             # | final reduce to handle renaming
             .[$k | sub("^_+";"")] = $n[$k]  # | any keys that start with _
           )                                 # /
      ]
    

提交回复
热议问题