首页 > 解决方案 > 附加到 curl 对象并使用 jq 添加到文件中的数组?

问题描述

我正在尝试从 URL 动态生成对象的 json 数组并将数组保存到文件中。是否可以通过管道将 curls 输出传递给 jq 修改 curl json 对象,然后将其附加到文件中的 json 列表并将更新的列表保存回文件?

目标是遍历一个列表并点击一个 URL,将对象拉下,然后向对象添加一个字段并将输出写入单个 json 文件。

首先,我们 curl url 以获取用户对象并向其添加一个新字段。

curl -s https://jsonplaceholder.typicode.com/users/1 | jq '. + {"level": 15}'

显示添加到对象的内容的差异。

diff <(curl -s https://jsonplaceholder.typicode.com/users/1 | jq . ) <(curl -s https://jsonplaceholder.typicode.com/users/1 | jq '. + {"level": 15}')

其次,我们将该用户对象添加到 userList.json 中的列表中。这是我被难住的部分。

第三,我们将更新后的列表写回文件。

JQ updated list command > userList.json

用户对象的卷曲 URL,将值附加到用户对象并将用户对象附加到文件中的数组。我尝试使用--argjson fileInfo "$(<userList.json)"但似乎无法让它工作。我收到无效的路径表达式或无法将对象添加到数组的其他错误。我已经尝试过,|= . +但无法弄清楚如何正确引用这两个数据集。

echo -e "[\n]" > userList.json
for i in {1..4}; do
  echo -e "\n==> User ${i}"
  testUrl=https://jsonplaceholder.typicode.com/users/${i}
  curl -s ${testUrl} | jq --argjson fileData "$(<userList.json)" '. + {level: 15} += [$fileData]' > userList.json
done
echo -e "==> Complete"
jq . userList.json

创建文件的非 json 方式,但它会丢失每个用户对象之间的逗号分隔符,我可以以编程方式添加逗号,但我宁愿找出 jq 并让它编写正确的 json。将此循环的输出与下面的预期数据进行比较。

for i in {1..4}; do 
  testUrl=https://jsonplaceholder.typicode.com/users/${i}
  curl -s ${testUrl} | jq '. + {level: 15}' >> userList.json
done

userList.json 文件的预期结果,它是添加了字段级别的 4 个用户对象。

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    },
    "level": 15
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "Shanna@melissa.tv",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    },
    "level": 15
  },
  {
    "id": 3,
    "name": "Clementine Bauch",
    "username": "Samantha",
    "email": "Nathan@yesenia.net",
    "address": {
      "street": "Douglas Extension",
      "suite": "Suite 847",
      "city": "McKenziehaven",
      "zipcode": "59590-4157",
      "geo": {
        "lat": "-68.6102",
        "lng": "-47.0653"
      }
    },
    "phone": "1-463-123-4447",
    "website": "ramiro.info",
    "company": {
      "name": "Romaguera-Jacobson",
      "catchPhrase": "Face to face bifurcated interface",
      "bs": "e-enable strategic applications"
    },
    "level": 15
  },
  {
    "id": 4,
    "name": "Patricia Lebsack",
    "username": "Karianne",
    "email": "Julianne.OConner@kory.org",
    "address": {
      "street": "Hoeger Mall",
      "suite": "Apt. 692",
      "city": "South Elvis",
      "zipcode": "53919-4257",
      "geo": {
        "lat": "29.4572",
        "lng": "-164.2990"
      }
    },
    "phone": "493-170-9623 x156",
    "website": "kale.biz",
    "company": {
      "name": "Robel-Corkery",
      "catchPhrase": "Multi-tiered zero tolerance productivity",
      "bs": "transition cutting-edge web services"
    },
    "level": 15
  }
]

标签: arraysjsoncurljq

解决方案


如果您知道在 forloop 中使用 jq with 将用户数据写入文件中的数组的更好解决方案,请告诉我。我真的很想了解 jq 是否可行。

如果我不必写入临时文件而是可以从文件中读取附加新的 curl 用户数据并在 for 循环中写回文件,我会更喜欢,但我会以这种方式获得结果和正确的 json。

我使用 JQ 的半体面解决方案。我最终编写了多个用户文件,然后将它们与 slurping 结合在一起。

for i in {1..4}; do
  echo -e "==> Creating a temp user json file for user${i}."
  testUrl=https://jsonplaceholder.typicode.com/users/${i}
  curl -s ${testUrl} | jq '. + {level: 15}' >> /tmp/temp_user_${i}_information_file.json
done

echo -e "==> Creating the final users json file."
jq -s '.' /tmp/temp_user_*_information_file.json > userList.json

推荐阅读