Building a JSONItem from a string

Hi everyone. I am trying to build a JSONItem out of a string. I’m following the sample code from the Language Reference where it says

dim js as string = “{”“Name”":"“John Doe”","“Age”":32,"“Kids”":["“Jonah”","“John Jr”"],"“Married”":true,"“Spouse”":"“Jane Doe”"}"
dim j as new JSONItem(js)
[…]
Foo.append(j)

This is fine - works well. However, I also want to use a variable in the string; say, replace Name with Name 4, where 4 is a counter. But I’m unfamiliar with this approach to building a string. That is, I don’t understand the significance of putting each separate part of this string in double quotes, nor why some parts are handled without double quotes (e.g. “Age”":32," as opposed to “Age”":"“32” which seems more consistent).

So two things really; first, how would I include a variable value in constructing this string, and second, can someone point me to some documentation that explains how these strings should be built?

Strings are concatenated with

s = s + “some string”

You should get comfortable first with manipulating strings and arrays.

Your data usually lives in a dictionary. You loop over the data in the dictionary and create the json out of the keys and values of a dictionary.

My data is not in a dictionary. I simply want to create a JSONItem that has this: {“identity”:“Connection 4”,“foo”:“bar”}. I am able to create this: {“identity”:“Connection”,“foo”:“bar”} but I want to be able to put in a variable (4 in this case). I just cannot work out the syntax for building that bit of the string. I think that the double quotes denote a string that contains a quoted string, but I’m not sure how then to create a quoted string that contains a variable value.

Quoting is a PITA.

You could do a replace.

dim js as string = "{""Name"":""John Doe"",""Age"":32,""Kids"":[""Jonah"",""John Jr""],""Married"":true,""Spouse"":""myvalue here""}" js = replace(js, "my value here", somevariable)

or

dim js as string = "{""Name"":""" + somevariable + """}"

The quotes are doubled so that you need 3 quotes - I hope. Code is untested.

Number, booleans, and nils are not quoted. Keys and string values are. Objects (equivalent to Dictionaries) are in the form {"key1":value,"key2":value} and arrays are [value1,value2, value3]. All JSON must either be an object or an array at the topmost level and values can be numeric, boolean (“true”, “false”), “null”, a string, an object, or an array. Strings are surrounded by double-quotes and double-quotes are escaped as \".

See http://json.org

Having said all that, why not just start with a JSONItem to begin and generate the string from that? You can easily add a value with a placeholder like nil and fill it in later.

One more thing: whitespace within the JSON is ignored.

Thanks folks. After a bit of a struggle I found the right syntax. For anyone else who might hit their heads against this particular brick wall, here is the code that worked.

dim connectionNumber as Integer = Preferences.mPreferences.child(“ppInstances”).count + 1
dim js As String = “{”“UserID”":"“Your UserID”","“ServerFriendlyName”":"“Connection " + str(connectionNumber) + “””,"“ServerURL”":"“URL for this connection”","“ServerDescription”":"“Optional description for this server connection”"}"
dim j as new JSONItem(js)
Preferences.mPreferences.child(“ppInstances”).append(j)

The crucial part is ““ServerFriendlyName””:"“Connection " + str(connectionNumber) + “””

As Beatrix suggests, it needs triple quotes. Mind-bending.

BTW; I agree about using a JSONItem. This is what I am doing; the preferences are held in JSON format in a file and then read into a Preferences object of type JSONItem.

You should not create a JSONItem like that. Create it like in the docs – where you have copied your first example from –, element by element with Value, Insert, Append, etc. Needs a few lines more, but is much better. It is much more readable and future proof – meaning you and anybody else will understand the code in a few weeks or months from now.

And it would have been easy to change the one element:

... item.Value("ServerFriendlyName") = "Connection " + Str(connectionNumber) ...
I would use Dim js As New JSONItem(someJSONString) only for JSON strings coming from files, databases, sockets, etc.

Your code should look something like

dim j as new JSONItem j.Value("UserID") = "Your UserID" j.Value("ServerFriendlyName") = "Connection " + Str(ConnectionNumber) j.Value("ServerURL") = "URL for this connection" j.Value("ServerDescription") = "Optional description for this server connection" Preferences.mPreferences.child("ppInstances").append(j)

There is little reason to write your own JSON string like that.

“There is little no reason to write your own JSON string like that.”

Fixed that for ya. :slight_smile: