Skip to content

Commit

Permalink
[feat] add json2couch from jbids toolbox
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Mar 28, 2024
1 parent 6da4f2b commit b39c374
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Contents.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
% jdlink - data = jdlink(uripath)
% jload - jload
% jsave - jsave
% json2couch - response = json2couch(jsonfile, couchdburl, dbname, docname, logininfo)
% jsoncache - [cachepath, filename]=jsoncache(hyperlink)
% jsonget - json=jsonget(fname,mmap,'$.jsonpath1','$.jsonpath2',...)
% jsonopt - val=jsonopt(key,default,optstruct)
Expand Down Expand Up @@ -57,4 +58,4 @@
% nii2jnii - jnii=nii2jnii(niifile)
% niicodemap - newval=niicodemap(name, value)
% niiformat - niiheader=niiformat(format)
% niiheader2jnii - nii = niiheader2jnii(nii0)
% niiheader2jnii - nii = niiheader2jnii(nii0)
1 change: 1 addition & 0 deletions INDEX
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ JSON Mmap
External Links
jdlink
jsoncache
json2couch
Compression and Decompression
base64decode
base64encode
Expand Down
61 changes: 61 additions & 0 deletions json2couch.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function response = json2couch(jsonfile, couchdburl, dbname, docname, options)
%
% json2couch(jsonfile, servername, dbname, docname, options)
%
% uploading JSON-encoded data to a CouchDB database (a NoSQL database)
% as a document
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% jsonfile: the path to the .json file
% couchdburl: the URL of the CouchDB server, usually it is
% http://servername:5984 where servername is your own
% server's domain name
% dbname: the database for whcih the file is uploaded to, must be
% created first
% docname: the document name for the uploaded JSON data
% options: a options structure created by weboptions(), defining
% Username, Password, ContentType when such information is
% desired to access the server; by default,
% options.ContentType is set to 'json' and
% options.RequestMethod is set to 'POST'
%
% if options is a string, it defines a template for a curl
% command, for example 'curl -X PUT -d @$f $u/$d/$s'.
% Variables (start with $) are expanded as
% $f -> path of the json file (first input)
% $u -> couchdb full URL (second input)
% $d -> database name (third input)
% $s -> document name (forth input)
%
% examples:
% values = inputdlg({'Username:', 'Password:'});
% options = weboptions('ContentType', 'json', 'RequestMethod', 'POST', 'Username',values{1},'Password',values{2});
% json2couch('ds001.json', 'https://example.com:5984', 'bids-samples', 'ds001', options)
% json2couch('ds001.json', sprintf('https://%s:%[email protected]:5984', values{1}, values{2}), 'bids-samples', 'ds001', 'curl -X PUT -d @$f $u/$d/$s')
%
% license:
% BSD license, see LICENSE_BSD.txt files for details
%
% -- this function is part of JBIDS toolbox (https://neurojson.org/#software)
%

if (nargin < 5)
options = weboptions('');
end

if (~ischar(options) && ~isa(options, 'string'))
options.ContentType = 'json';
options.RequestMethod = 'POST';
response = webwrite([couchdburl '/' dbname '/' docname], fileread(jsonfile), options);
else
options = regexprep(options, '\$f', ['''' jsonfile '''']);
options = regexprep(options, '\$u', couchdburl);
options = regexprep(options, '\$d', dbname);
options = regexprep(options, '\$s', docname);
[status, response] = system(options);
if (status ~= 0)
error('command failed:\n%s\n', response);
end
end
1 change: 1 addition & 0 deletions jsonlab.prj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Please note that data files produced by `saveubjson` may utilize a special "opti
<file>${PROJECT_ROOT}/jdlink.m</file>
<file>${PROJECT_ROOT}/jload.m</file>
<file>${PROJECT_ROOT}/jsave.m</file>
<file>${PROJECT_ROOT}/json2couch.m</file>
<file>${PROJECT_ROOT}/jsoncache.m</file>
<file>${PROJECT_ROOT}/jsonget.m</file>
<file>${PROJECT_ROOT}/jsonhash.m</file>
Expand Down

0 comments on commit b39c374

Please sign in to comment.