问题
My backup.json
looks like this:
{
"andres": [
[
{
"id": 1,
"email": "password",
"username": test1,
"password": "email@email.com",
"name": "Dummy Account",
"address": "123 st road",,
"ip_address": "0.0.0.0",
"phone": "123-123-1234",
},
{
"id": 2,
"email": "email2@email.com",
"username": test2,
"password": "password",
"name": "Dummy Account",
"address": "123 st road",,
"ip_address": "0.0.0.0",
"phone": "123-123-1234"
}
],
]
}
I'm using the command:
jq -r '.andres[] | .id, .email, .username, .password, .name, .address, .ip_address, .phone' < backup.json > backup.csv
But it gives the error:
Cannot index array with string "id"
I want it to look like this:
1,email@email.com,test1,password,Dummy Account,123 st road,0.0.0.0,123-123-1234
2,email@email.com,test2,password,Dummy Account,123 st road,0.0.0.0,123-123-1234
I'm new to using JQ. Can someone please fix my command and tell me where i went wrong?
Thanks!
回答1:
The value of andres
in your json is an array of arrays, but you're accessing it as if it was an array of objects. You would have to flatten the arrays first (or index into) to access the objects. Then from those objects, you will want to map the values you want as csv as an array of values.
$ jq -r '
.andres[][] | [.id, .email, .username, .password, .name, .address, .ip_address, .phone] | @csv
' < backup.json > backup.csv
Note the second set of []
in .andres[][]
.
You may want to add some headers to your output as well.
$ jq -r '
["id", "email", "username", "password", "name", "address", "ip_address", "phone"] as $headers
| $headers, (.andres[][] | [.[$headers[]]]) | @csv
' < backup.json > backup.csv
回答2:
jq is a funny beast. I find it takes a lot of trial and error.
After fixing your JSON
$ jq -r '.andres[][] | map(values) | @csv' file.json
1,"password","test1","email@email.com","Dummy Account","123 st road","0.0.0.0","123-123-1234"
2,"email2@email.com","test2","password","Dummy Account","123 st road","0.0.0.0","123-123-1234"
Note that for id=1, you have the password and email values switched.
See also "Format strings and escaping" in the jq manual
回答3:
Except that this was not valid json (there are additional ,
at "phone": "123-123-1234",
) but after removing it I was able to get a valid json here.
There are many more arrays [ { .. } ]
in your json file, you need to get that into account with [][][]
.
The following json:
{
"Emails": [
{
"email@email.com": [
{
"andres": [
[
{
"id": 1,
"email": "email@email.com",
"username": "test1",
"password": "password",
"name": "Dummy Account",
"address": "123 st road",
"ip_address": "0.0.0.0",
"phone": "123-123-1234"
},
{
"id": 2,
"email": "email@email.com",
"username": "test2",
"password": "password",
"name": "Dummy Account",
"address": "123 st road",
"ip_address": "0.0.0.0",
"phone": "123-123-1234"
}
],
true
]
}
]
}
]
}
with the following filter:
.Emails[][][].andres[][] | .id, .email, .username, .password, .name, .address, .ip_address, .phone
gives me:
jq: error (at <stdin>:34): Cannot iterate over boolean (true)
1
"email@email.com"
"test1"
"password"
"Dummy Account"
"123 st road"
"0.0.0.0"
"123-123-1234"
2
"email@email.com"
"test2"
"password"
"Dummy Account"
"123 st road"
"0.0.0.0"
"123-123-1234"
exit status 5
on jqplay. I don't know what to do with that true
in json, it's not valid, it should be an array member, like { "true" }
.
来源:https://stackoverflow.com/questions/52125879/jq-cannot-index-array-with-string-id