简体   繁体   English

使用 sed 或 jq 删除动态生成的 JSON 数组中的最后一个 `},`

[英]Remove last `},` in a dynamically generated JSON array using sed or jq

I am attempting to dynamically generate a JSON, however, the method I follow leaves a last }, making the JSON invalid:我正在尝试动态生成 JSON,但是,我遵循的方法最后留下了},使 JSON 无效:

#Initializing Current State file
    echo "[" > state.json
    # Getting list of all EKS clusters
    for cluster in $(aws eks list-clusters --output text | awk '{print $2}')
    do
      echo "====== $cluster ======"
      # Get list of all ASG from cluster
      for asg in $(aws autoscaling describe-auto-scaling-groups  --query 'AutoScalingGroups[?contains(Tags[?Key==`eks:cluster-name`].Value, `'"$cluster"'`)].[AutoScalingGroupName]' --output text)
      do
        echo "Getting capacity for ASG: $asg"
        MinSize=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $asg --query 'AutoScalingGroups[*].MinSize' --output text)
        DesiredCapacity=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $asg --query 'AutoScalingGroups[*].DesiredCapacity' --output text)  
        MaxSize=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $asg --query 'AutoScalingGroups[*].MaxSize' --output text)
        JSON_STRING=$( jq -n \
                  --arg cn "$cluster" \
                  --arg asg "$asg" \
                  --arg MinSize "$MinSize" \
                  --arg DesiredCapacity "$DesiredCapacity" \
                  --arg MaxSize "$MaxSize" \
                  '{cluster: $cn, asgname: $asg, minSize: $MinSize, DesiredCapacity: $DesiredCapacity, MaxSize: $MaxSize}' )
        echo "${JSON_STRING}," >> state.json
      done
    done
    #Finalizing Current State file
    echo "]" >> state.json
$ cat state.json
[
{
  "cluster": "eks-b",
  "asgname": "eks-1234",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
},
{
  "cluster": "eks-b",
  "asgname": "eks-1234",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
},
{
  "cluster": "dev",
  "asgname": "eks-12345",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
},
{
  "cluster": "dev",
  "asgname": "eks-1234",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
},
{
  "cluster": "eks-a",
  "asgname": "eks-1234",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
},
{
  "cluster": "eks-a",
  "asgname": "eks-1323",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
},
]

jq validation: jq验证:

$ cat state.json  | jq empty
parse error: Expected another array element at line 44, column 1

I have attempted multiple options, mostly with sed , but have not met the specific requirement to only remove the last occurrence of }, Any advice is appreciated.我尝试了多种选择,主要是sed ,但没有满足仅删除最后一次出现的},欢迎提供任何建议。

Let your script just produce a stream of objects by not printing the brackets and the commas.通过不打印括号和逗号,让您的脚本只生成 stream 个对象。

echo "${JSON_STRING}" >> state.json

state.json should then contain a stream of objects: state.json应该包含一个 stream 的对象:

{
  "cluster": "eks-b",
  "asgname": "eks-1234",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
}
{
  "cluster": "eks-b",
  "asgname": "eks-1234",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
}
{
  "cluster": "dev",
  "asgname": "eks-12345",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
}
{
  "cluster": "dev",
  "asgname": "eks-1234",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
}
{
  "cluster": "eks-a",
  "asgname": "eks-1234",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
}
{
  "cluster": "eks-a",
  "asgname": "eks-1323",
  "minSize": "0",
  "DesiredCapacity": "0",
  "MaxSize": "0"
}

Now you can read in that stream with jq, using the -s option to convert it into a proper array.现在您可以使用 jq 读取 stream,使用-s选项将其转换为适当的数组。

jq -s . state.json
[
  {
    "cluster": "eks-b",
    "asgname": "eks-1234",
    "minSize": "0",
    "DesiredCapacity": "0",
    "MaxSize": "0"
  },
  {
    "cluster": "eks-b",
    "asgname": "eks-1234",
    "minSize": "0",
    "DesiredCapacity": "0",
    "MaxSize": "0"
  },
  {
    "cluster": "dev",
    "asgname": "eks-12345",
    "minSize": "0",
    "DesiredCapacity": "0",
    "MaxSize": "0"
  },
  {
    "cluster": "dev",
    "asgname": "eks-1234",
    "minSize": "0",
    "DesiredCapacity": "0",
    "MaxSize": "0"
  },
  {
    "cluster": "eks-a",
    "asgname": "eks-1234",
    "minSize": "0",
    "DesiredCapacity": "0",
    "MaxSize": "0"
  },
  {
    "cluster": "eks-a",
    "asgname": "eks-1323",
    "minSize": "0",
    "DesiredCapacity": "0",
    "MaxSize": "0"
  }
]

Demo for the jq -s part jq -s部分的演示

If you don't require the final result to be pretty-printed, you could save the expense of the call to jq -s by deleting the superfluous comma, eg如果您不要求最终结果被漂亮地打印出来,您可以通过删除多余的逗号来节省调用jq -s的费用,例如

sed '$s/,$//'

or if you like:或者如果你喜欢:

sed -i.bak '$s/,$//'

Even better, you can avoid handling the terminating ']' in the same step, eg using '$s/,$/]/'更好的是,您可以避免在同一步骤中处理终止 ']',例如使用 '$s/,$/]/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM