简体   繁体   English

如何使用 shell 脚本将 json 对象从文件上传到 couchdb?

[英]How to upload json objects from a file to couchdb using a shell script?

Sample json array样品 json 阵列

[
{
   id : 1,
   name : Amit,
   age : 18,
   inSchool : 1;
}, 
{
   id : 2,
   name : Sunil,
   age : 21,
   inSchool : 0;
}, 
{
   id : 3,
   name : Anil,
   age : 17,
   inSchool : 1;
}
]

I want ->我要->

Loop over this JSON array from file and stores these json objects in couchdb.从文件中循环这个 JSON 数组并将这些 json 对象存储在 couchdb 中。

If there's any good way to do this.如果有什么好的方法可以做到这一点。 Please help me.请帮我。

For a real JSON file, like:对于真正的 JSON 文件,例如:

[
    {
        "id": 1,
        "name": "Amit",
        "age": 18,
        "inSchool": 1
    },
    {
        "id": 2,
        "name": "Sunil",
        "age": 21,
        "inSchool": 0
    },
    {
        "id": 3,
        "name": "Anil",
        "age": 17,
        "inSchool": 1
    }
]

you can loop over this JSON array using script based on:您可以使用基于以下的脚本循环遍历此 JSON 数组:

jq -r '.[] | "\(.id) \(.age) \(.inSchool) \(.name)"' fixed.json | \
while read -r    id     age     inSchool     name
do
    echo "- item[id = ${id}]: name: ${name}, age: ${age}, inSchool: ${inSchool}"

    # Add command to upload to couchdb

done

output: output:

➜ bash script.sh
 - item[id = 1]: name: Amit, age: 18, inSchool: 1
 - item[id = 2]: name: Sunil, age: 21, inSchool: 0
 - item[id = 3]: name: Anil, age: 17, inSchool: 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM