0

I have a workspace with a bunch of variables I'd like to turn into a JSON document using JSONlab. My code so far looks like this:

loadjason = {'{"Duration":Duration,"Num_Samples":nsamples,"Frequency":Freq,"Num_Channels":nchannels1}')

The items not in double quotes (i.e. Freq) are variables from my MATLAB workspace but my output is:

Error using loadjson>error_pos (line 482)
JSONparser:invalidFormat: Value expected at position 13:
{"Duration":<error>Duration,"Num_Samples

Error in loadjson>parse_value (line 471)
   error_pos('Value expected at position %d');

Error in loadjson>parse_object (line 206)
   val = parse_value(varargin{:});

Error in loadjson (line 96)
   data{jsoncount} = parse_object(opt);`

How do I pass variables into loadjson so that they are read as their values and not as literals?

1 Answer 1

2
loadjason = {'{"Duration":Duration,"Num_Samples":nsamples,"Frequency":Freq,"Num_Channels":nchannels1}')

is not valid JSON. String values, such as Duration, need to be in quotes if they are strings (e.g., '"Duration":"5 seconds"'). Numeric values don't need quotes. E.g., '"Duration":5' is valid. Also your outermost brackets don't match, and you don't need { } enclosing the entire string twice. Modify your MATLAB code to produce something like this,

loadjason = '{"Duration":5,"Num_Samples":1,"Frequency":1,"Num_Channels":1}'

To take int values:

loadjason = '{"Duration":'
temp = int2str(Duration)
loadjason = strcat(loadjason,temp)

loadjason = '{"Num_Samples":'
temp = int2str(nsamples)
loadjason = strcat(loadjason,temp)

loadjason += ',{"Frequency":'
temp = int2str(freq)
loadjason = strcat(loadjason,temp)

loadjason += ',{"Num_Channels":'
temp = int2str(nchannels1)
loadjason = strcat(loadjason,temp)

loadjason += '}'

and continue for the rest of the variables. Alternately you could create a function that does this for you.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there no way to pass the value of a variable? My MATLAB variable 'Duration' is an int but if I write "Duration" won't it interpret it as the string "Duration" as opposed to the value of the variable named "Duration"?
You can use int2str to convert the value of an integer into a string. I'll add that to my solution.
Thanks, this worked. Side question: do you know a good way to convert doubles to strings? The variable Duration is a double with the value 1.6420e+04. I've tried sprintf('%f',Duration) and num2str but neither of them give the intended answer of "1.6420e+04". I need a good way to make that conversion OR convert it to a more readable float.
check out this link mathworks.com/help/matlab/ref/num2str.html I believe you can use this to get whatever precision you need. Make sure to pass the desired precision as an positive int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.