Skip to content

Commit b39c374

Browse files
committed
[feat] add json2couch from jbids toolbox
1 parent 6da4f2b commit b39c374

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

Contents.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
% jdlink - data = jdlink(uripath)
1919
% jload - jload
2020
% jsave - jsave
21+
% json2couch - response = json2couch(jsonfile, couchdburl, dbname, docname, logininfo)
2122
% jsoncache - [cachepath, filename]=jsoncache(hyperlink)
2223
% jsonget - json=jsonget(fname,mmap,'$.jsonpath1','$.jsonpath2',...)
2324
% jsonopt - val=jsonopt(key,default,optstruct)
@@ -57,4 +58,4 @@
5758
% nii2jnii - jnii=nii2jnii(niifile)
5859
% niicodemap - newval=niicodemap(name, value)
5960
% niiformat - niiheader=niiformat(format)
60-
% niiheader2jnii - nii = niiheader2jnii(nii0)
61+
% niiheader2jnii - nii = niiheader2jnii(nii0)

INDEX

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ JSON Mmap
2828
External Links
2929
jdlink
3030
jsoncache
31+
json2couch
3132
Compression and Decompression
3233
base64decode
3334
base64encode

json2couch.m

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function response = json2couch(jsonfile, couchdburl, dbname, docname, options)
2+
%
3+
% json2couch(jsonfile, servername, dbname, docname, options)
4+
%
5+
% uploading JSON-encoded data to a CouchDB database (a NoSQL database)
6+
% as a document
7+
%
8+
% author: Qianqian Fang (q.fang <at> neu.edu)
9+
%
10+
% input:
11+
% jsonfile: the path to the .json file
12+
% couchdburl: the URL of the CouchDB server, usually it is
13+
% http://servername:5984 where servername is your own
14+
% server's domain name
15+
% dbname: the database for whcih the file is uploaded to, must be
16+
% created first
17+
% docname: the document name for the uploaded JSON data
18+
% options: a options structure created by weboptions(), defining
19+
% Username, Password, ContentType when such information is
20+
% desired to access the server; by default,
21+
% options.ContentType is set to 'json' and
22+
% options.RequestMethod is set to 'POST'
23+
%
24+
% if options is a string, it defines a template for a curl
25+
% command, for example 'curl -X PUT -d @$f $u/$d/$s'.
26+
% Variables (start with $) are expanded as
27+
% $f -> path of the json file (first input)
28+
% $u -> couchdb full URL (second input)
29+
% $d -> database name (third input)
30+
% $s -> document name (forth input)
31+
%
32+
% examples:
33+
% values = inputdlg({'Username:', 'Password:'});
34+
% options = weboptions('ContentType', 'json', 'RequestMethod', 'POST', 'Username',values{1},'Password',values{2});
35+
% json2couch('ds001.json', 'https://example.com:5984', 'bids-samples', 'ds001', options)
36+
% json2couch('ds001.json', sprintf('https://%s:%[email protected]:5984', values{1}, values{2}), 'bids-samples', 'ds001', 'curl -X PUT -d @$f $u/$d/$s')
37+
%
38+
% license:
39+
% BSD license, see LICENSE_BSD.txt files for details
40+
%
41+
% -- this function is part of JBIDS toolbox (https://neurojson.org/#software)
42+
%
43+
44+
if (nargin < 5)
45+
options = weboptions('');
46+
end
47+
48+
if (~ischar(options) && ~isa(options, 'string'))
49+
options.ContentType = 'json';
50+
options.RequestMethod = 'POST';
51+
response = webwrite([couchdburl '/' dbname '/' docname], fileread(jsonfile), options);
52+
else
53+
options = regexprep(options, '\$f', ['''' jsonfile '''']);
54+
options = regexprep(options, '\$u', couchdburl);
55+
options = regexprep(options, '\$d', dbname);
56+
options = regexprep(options, '\$s', docname);
57+
[status, response] = system(options);
58+
if (status ~= 0)
59+
error('command failed:\n%s\n', response);
60+
end
61+
end

jsonlab.prj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Please note that data files produced by `saveubjson` may utilize a special "opti
114114
<file>${PROJECT_ROOT}/jdlink.m</file>
115115
<file>${PROJECT_ROOT}/jload.m</file>
116116
<file>${PROJECT_ROOT}/jsave.m</file>
117+
<file>${PROJECT_ROOT}/json2couch.m</file>
117118
<file>${PROJECT_ROOT}/jsoncache.m</file>
118119
<file>${PROJECT_ROOT}/jsonget.m</file>
119120
<file>${PROJECT_ROOT}/jsonhash.m</file>

0 commit comments

Comments
 (0)