我正在尝试从URL动态生成对象的json数组,并将数组保存到文件中。是否可以将卷发输出输送到jq,修改curls对象,然后将其附加到文件中的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用户对象的Curl 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
doneuserList.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
}
]发布于 2018-12-06 04:25:16
如果您知道更好的解决方案,使用JQwithinthefor循环将用户数据写入文件中的数组中,请告诉我。我真的很有兴趣了解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.jsonhttps://stackoverflow.com/questions/53625680
复制相似问题