簡體   English   中英

在bash腳本或jq中轉義json標簽

[英]Escape json tags in bash script or jq

我有一個 YAML 文件,我正在使用yq將其轉換為 JSON 文件。 它生成以下 output,

{
  "a" : 1,
  "b" : 2,
  "c" : {
  "id": "9ee ...",
  "parent": "abc..."
  }
}

然后我根據上面的 JSON 鍵和值創建另一個 JSON。 請找到下面的代碼片段,

# Extract the properties from the YAML file
json=$(yq -j "$file_name")

# Iterate over the properties
parameters=""
for key in $(echo "${json}" | jq -r 'keys[]'); do

    # Extract the key and value of the property
    value=$(echo "${json}" | jq -r ".$key")
    echo "Adding parameter $key with value $value to SSM"

    # Add the property to the list of parameters
    parameters+="{\"Name\": \"$key\", \"Value\": \"$value\", \"Type\": \"String\", \"Overwrite\": true}"
done

由於第一個 JSON 中的值已經是 JSON,因此我們無法生成第二個 JSON,它會生成無效 JSON 錯誤。

有什么辦法可以轉義/字符串化bash scriptjq中的 JSON 個字符,以便我們能夠生成第二個 JSON?

任何幫助將非常感激。

實際 output:

[
{
"Name": "a",
"Value": "1",
"Type": "String",
"Overwrite": "true"
},
{
"Name": "b",
"Value": "2",
"Type": "String",
"Overwrite": "true"
},
{
"Name": "c",
"Value": "{
      "id": "9ee ...",
      "parent": "abc..."
      }",
"Type": "String",
"Overwrite": "true"
}
]

上面的不是有效的 JSON。

預計 output:

[
{
"Name": "a",
"Value": "1",
"Type": "String",
"Overwrite": "true"
},
{
"Name": "b",
"Value": "2",
"Type": "String",
"Overwrite": "true"
},
{
"Name": "c",
"Value": "{\r\n      \"id\": \"9ee ...\",\r\n      \"parent\": \"abc...\"\r\n      }",
"Type": "String",
"Overwrite": "true"
}
]

為什么要嘗試使用 shell 循環( 通常應避免)而不是直接使用 jq 來模擬 jq 的行為?

yq -j "$file_name" | jq 'to_entries | map({
    Name: .key,
    Value: .value,
    Type: "String",
    Overwrite: true
})'

或僅使用yq直接轉換:

yq 'to_entries | map({
    Name: .key,
    Value: .value,
    Type: "String",
    Overwrite: true
})' -j "$file_name"

澄清問題編輯后更新:很明顯你想將值轉換為字符串。 jq 有tostring過濾器,程序因此變成:

to_entries | map({
    Name: .key,
    Value: (.value | tostring),
    Type: "String",
    Overwrite: true
})

請注意,這不會保留換行符和縮進,但會以“緊湊”的方式格式化 JSON object。 讓我們知道這是否有問題。

$ jq 'to_entries | map({
    Name: .key,
    Value: (.value | tostring),
    Type: "String",
    Overwrite: true
})' <<JSON
{
  "a": 1,
  "b": 2,
  "c": {
    "id": "9ee ...",
    "parent": "abc..."
  }
}
JSON
[
  {
    "Name": "a",
    "Value": "1",
    "Type": "String",
    "Overwrite": true
  },
  {
    "Name": "b",
    "Value": "2",
    "Type": "String",
    "Overwrite": true
  },
  {
    "Name": "c",
    "Value": "{\"id\":\"9ee ...\",\"parent\":\"abc...\"}",
    "Type": "String",
    "Overwrite": true
  }
]

這應該達到預期的效果:

jq 'to_entries | map({
    Name: .key,
    Value: (.value|if (type == "object")
                   then tojson
                   else tostring
                   end),
    Type: "String",
    Overwrite: true})
' input.json

要從 jq 中對數據進行 JSON 編碼,您可以使用tojson (或@json )內置函數。

此外,要獲取某些數據的實際類型,可以使用type function。

也許你正試圖完成這樣的事情:

# Convert the the YAML file into JSON
json="$(yq -j "$file_name")"

# Transform the JSON data into desired format
jq 'to_entries | map({Name: .key} + (.value | {
      EncodedValue: tojson,
      OriginalType: type,
      Overwrite:    true
    }))' <<< "$json" > "$new_file_name"

檢查新文件的內容現在會給你類似的東西:

[
  {
    "Name": "a",
    "EncodedValue": "1",
    "OriginalType": "number",
    "Overwrite": true
  },
  {
    "Name": "b",
    "EncodedValue": "2",
    "OriginalType": "number",
    "Overwrite": true
  },
  {
    "Name": "c",
    "EncodedValue": "{\"id\":\"9ee ...\",\"parent\":\"abc...\"}",
    "OriginalType": "object",
    "Overwrite": true
  }
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM