作为我正在使用的模拟工具的回应,我收到了以下答复。

{
  "mappings" : [
{
"id" : "bcf3559f-7ff7-406b-a4f1-6d3e9ac00e63",
"name" : "Hellow world 2",
"request" : {
  "url" : "/hello-world-2",
  "method" : "POST"
},
"response" : {
  "status" : 200,
  "body" : "\nBody content for stub 3\n\n",
  "headers" : { }
},
"uuid" : "bcf3559f-7ff7-406b-a4f1-6d3e9ac00e63",
"persistent" : true,
"priority" : 5
  },
{
"id" : "9086b24f-4f5e-465a-bbe5-73bbfb82cd5c",
"name": "Hello world",
"request" : {
  "url" : "/hello-world",
  "method" : "ANY"
},
"response" : {
  "status" : 200,
  "body" : "Hi!"
},
"uuid" : "9086b24f-4f5e-465a-bbe5-73bbfb82cd5c"
} ]
}


我想知道如何使用对象ID命名的文件将每个对象拆分为自己的文件。

例如:

bcf3559f-7ff7-406b-a4f1-6d3e9ac00e63.json

bcf3559f-7ff7-406b-a4f1-6d3e9ac00e63.json

到目前为止,我已经了解到了这一点,但无法超越:

jq -c '.mappings = (.mappings[] | [.])' mappings.json |
  while read -r json ; do
  N=$((N+1))
  jq . <<< "$json"  > "tmp/file${N}.json"
done

最佳答案

我建议在一行上打印ID,在下一行上打印相应的对象。例如:

jq -c '.mappings[] | .id, .' mappings.json |
    while read -r id ; do
    echo "id=$id"
    read -r json
      jq . <<< "$json"  > "tmp/${id}.json"
done

关于arrays - bash将数组拆分为具有动态名称的单独文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51970861/

10-12 12:57
查看更多