From b39c374cddfb7066a5252875852ea187c046e069 Mon Sep 17 00:00:00 2001 From: Qianqian Fang Date: Thu, 28 Mar 2024 15:16:40 -0400 Subject: [PATCH] [feat] add json2couch from jbids toolbox --- Contents.m | 3 ++- INDEX | 1 + json2couch.m | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ jsonlab.prj | 1 + 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 json2couch.m diff --git a/Contents.m b/Contents.m index 38b788a..5c62f8e 100644 --- a/Contents.m +++ b/Contents.m @@ -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) @@ -57,4 +58,4 @@ % nii2jnii - jnii=nii2jnii(niifile) % niicodemap - newval=niicodemap(name, value) % niiformat - niiheader=niiformat(format) -% niiheader2jnii - nii = niiheader2jnii(nii0) \ No newline at end of file +% niiheader2jnii - nii = niiheader2jnii(nii0) diff --git a/INDEX b/INDEX index 47ee492..bba8211 100644 --- a/INDEX +++ b/INDEX @@ -28,6 +28,7 @@ JSON Mmap External Links jdlink jsoncache + json2couch Compression and Decompression base64decode base64encode diff --git a/json2couch.m b/json2couch.m new file mode 100644 index 0000000..9bfb65e --- /dev/null +++ b/json2couch.m @@ -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 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:%s@example.com: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 diff --git a/jsonlab.prj b/jsonlab.prj index e0ef353..4cf9c29 100644 --- a/jsonlab.prj +++ b/jsonlab.prj @@ -114,6 +114,7 @@ Please note that data files produced by `saveubjson` may utilize a special "opti ${PROJECT_ROOT}/jdlink.m ${PROJECT_ROOT}/jload.m ${PROJECT_ROOT}/jsave.m + ${PROJECT_ROOT}/json2couch.m ${PROJECT_ROOT}/jsoncache.m ${PROJECT_ROOT}/jsonget.m ${PROJECT_ROOT}/jsonhash.m