diff --git a/mergestruct.m b/mergestruct.m
new file mode 100644
index 0000000000000000000000000000000000000000..ce344ad285dae4ad3dd0dc5e16455f21cf6c1950
--- /dev/null
+++ b/mergestruct.m
@@ -0,0 +1,33 @@
+function s=mergestruct(s1,s2)
+%
+% s=mergestruct(s1,s2)
+%
+% merge two struct objects into one
+%
+% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
+% date: 2012/12/22
+%
+% input:
+%      s1,s2: a struct object, s1 and s2 can not be arrays
+%
+% output:
+%      s: the merged struct object. fields in s1 and s2 will be combined in s.
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
+%
+
+if(~isstruct(s1) || ~isstruct(s2))
+    error('input parameters contain non-struct');
+end
+if(length(s1)>1 || length(s2)>1)
+    error('can not merge struct arrays');
+end
+fn=fieldnames(s2);
+s=s1;
+for i=1:length(fn)              
+    s=setfield(s,fn{i},getfield(s2,fn{i}));
+end
+
diff --git a/savejson.m b/savejson.m
index bc10dabaf30c67091c6159805e8b6ef948659f79..217b747b33f8c028fa483918dc301b822ec6a61e 100644
--- a/savejson.m
+++ b/savejson.m
@@ -57,7 +57,8 @@ if((isnumeric(obj) || islogical(obj) || ischar(obj)) && isempty(rootname))
     rootlevel=0;
     varname='';
 end
-json=obj2json(varname,obj,rootlevel,varargin{:});
+opt=varargin2struct(varargin{:});
+json=obj2json(varname,obj,rootlevel,opt);
 if(rootisarray)
     json=sprintf('%s\n',json);
 else
diff --git a/varargin2struct.m b/varargin2struct.m
new file mode 100644
index 0000000000000000000000000000000000000000..1dce180e2ff302eee92688eb20b33e0e02b167ea
--- /dev/null
+++ b/varargin2struct.m
@@ -0,0 +1,40 @@
+function opt=vargin2struct(varargin)
+%
+% opt=vargin2struct('param1',value1,'param2',value2,...)
+%   or
+% opt=vargin2struct(...,optstruct,...)
+%
+% convert a series of input parameters into a structure
+%
+% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
+% date: 2012/12/22
+%
+% input:
+%      'param', value: the input parameters should be pairs of a string and a value
+%       optstruct: if a parameter is a struct, the fields will be merged to the output struct
+%
+% output:
+%      opt: a struct where opt.param1=value1, opt.param2=value2 ...
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
+%
+
+len=length(varargin);
+opt=struct;
+if(len==0) return; end
+i=1;
+while(i<=len)
+    if(isstruct(varargin{i}))
+        opt=mergestruct(opt,varargin{i});
+    elseif(ischar(varargin{i}) && i<len)
+        opt=setfield(opt,varargin{i},varargin{i+1});
+        i=i+1;
+    else
+        error('input must be in the form of ...,''name'',value,... pairs or structs');
+    end
+    i=i+1;
+end
+