我正在尝试从URL动态生成对象的json数组,并将该数组保存到文件中。是否可以将curls输出传递给jq修改curl json对象,然后将其附加到文件中的json列表中,然后将更新后的列表保存回文件中?
目标是遍历列表并点击URL,将对象拉下,然后向该对象添加字段并将输出写入单个json文件。
首先,我们卷曲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
}
]
答案 0 :(得分:0)
如果您知道在forloop中使用jq和forfile将用户数据写入文件中的更好的解决方案,请告诉我。我真的很想了解jq是否可行。
如果我不必写临时文件,而是可以从文件中读取新的curl用户数据,然后在for循环内写回文件,我会更喜欢,但是我得到了结果和正确的json方式。
我使用JQ的半体面解决方案。我最终编写了多个用户文件,然后将它们组合在一起。
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