How to Add New Custom Field?
How to add a new custom field to an item already created via cli?
{
"object":"item",
"id":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"organizationId":null,
"folderId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"type":2,
"name":"TESTE",
"notes":"Teste",
"favorite":false,
"fields":[
{
"name":"Pwd-1",
"value":"!q2w3e4r5t",
"type":1
}
],
"secureNote":{
"type":0
},
"collectionIds":[
],
"revisionDate":"2020-10-08T17:37:10.760Z"
}
in the informed json a field is already added, but I added it via the web panel, I would like to add more fields, but via the command line, how should I proceed in this situation?
@sevensixseven
@AlanMartines Here is an example of an array edit to add a new element. The key is to use '+=' to add a new element and leave the existing elements in the array unchanged.
bw get item 0cca8b6b-bcf0-4bd7-b348-ac4f00d4e304 | jq '.fields+=[{"name":"custom3","value":"value3","type":0}]' | bw encode | bw edit item 0cca8b6b-bcf0-4bd7-b348-ac4f00d4e304
Please let me know if there is anything else I can help with.
I ran the command saying in PowerShell, but it shows the error message below.
jq : jq: error: syntax error, unexpected '-', expecting '}' (Windows cmd shell quoting issues?) at <top-level>, line 1:
No C:\Script\BW-UpdatePassword.ps1:219 caractere:52
+ ... e0164136c | jq '.fields+=[{"name":"Pwd-1","value":"12345678","type": ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (jq: error: synt...level>, line 1::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
.fields+=[{name:Pwd-1,value:12345678,type:1}]
jq: 1 compile error
bw : `requestJson` was not provided.
No C:\Script\BW-UpdatePassword.ps1:219 caractere:128
+ ... :1}]' | bw encode | bw edit item 6f289d19-d04f-465a-8df7-ac4e0164136c
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (`requestJson` was not provided.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
@AlanMartines I apologize, the previous example was for Bash. This is how you will want to write it for PowerShell.
jq '.fields+=[{name:"""custom3""",value:"""value3""",type:0}]'
Thank you very much, it worked perfectly.