-1

In Visual Basic code, when I try to insert a variable into this JSON string it comes back as a 400 bad request. How do I correctly feed this JSON string a variable?

Dim myJSON As String = "{""StoreId"":""12345"",""TerminalId"":""12345"",""CaptureMode"":""true"",""MerchantReferenceCode"":""VARIABLEINSERTEDHERE"",""InvoiceNumber"":""12345"",""TimeoutMinutes"":""5"",""ShowAddress"":""true"",""AddressRequired"":""true"",""TransactionTotal"":""33.33"",""TaxTotal"":""2.00"",""CustomerCode"":""12345""}"
  • how are you currently inserting your variable? – ps2goat Jul 12 '21 at 19:01
  • Does this answer your question? [Variable substitution into strings](https://stackoverflow.com/questions/2210574/variable-substitution-into-strings) – GSerg Jul 12 '21 at 19:01
  • Does this answer your question? [how to insert a variable in a middle of a text in vb](https://stackoverflow.com/q/34715234/11683) – GSerg Jul 12 '21 at 19:02
  • @GSerg Thank you, I tried this but since the placeholder uses curly braces it results in a placeholder error because the JSON is also using curly braces. – Rick Walker Jul 12 '21 at 19:27
  • 1
    @RickWalker It is much faster to google each individual small step yourself than to ask about every tiny obstacle. So you want to use `String.Format`, so see https://stackoverflow.com/q/91362/11683. Or don't use `String.Format` and use https://stackoverflow.com/a/34715258/11683. – GSerg Jul 12 '21 at 19:29
  • @GSerg Thank you, I should have look at escaping the curly braces. Thank you for your answer. – Rick Walker Jul 12 '21 at 19:33

2 Answers2

1

Your JSON as it sits right now is an Object that looks like this:

{
  "StoreId": "12345",
  "TerminalId": "12345",
  "CaptureMode": "true",
  "MerchantReferenceCode": "VARIABLEINSERTEDHERE",
  "InvoiceNumber": "12345",
  "TimeoutMinutes": "5",
  "ShowAddress": "true",
  "AddressRequired": "true",
  "TransactionTotal": "33.33",
  "TaxTotal": "2.00",
  "CustomerCode": "12345"
}

One option that you have is to create a new JObject, use the Add method (documentation) to build the object's properties, and then the ToString method (documentation) to serialize the object to JSON. This way you don't have to worry about properly formatting the JSON, just let the library do it for you.

Take a look at this example:

Dim myVariable = "VARIABLEINSTEREDHERE"
Dim request = New JObject()
request.Add("StoreId", 12345)
request.Add("TerminalId", 12345)
request.Add("CaptureMode", True)
request.Add("MerchantReferenceCode", myVariable)
request.Add("InvoiceNumber", 12345)
request.Add("TimeoutMinutes", 5)
request.Add("ShowAddress", True)
request.Add("AddressRequired", True)
request.Add("TransactionTotal", 33.33)
request.Add("TaxTotal", 2.00) ' taxation is theft
request.Add("CustomerCode", 12345)

Dim myJson = request.ToString()

Example: https://dotnetfiddle.net/FQDpVq

David
  • 5,877
  • 3
  • 23
  • 40
0

The short answer to the question you asked:

myjson = myjson.replace("VALUEPLACEHOLDER1","SoMeTHiNGeLSe")

Now, if you are talking about tricky pattern matching, then you will need to use regex if you want to approach this from a string-manipulation perspective. That sets you up for all sorts of unanticipated problems down the road with things you didn't anticipate, though, and you'll probably be happier in the long run if you embrace using JSON.

Try using the NewtonSoft JSON libraries. Then you can do something like:

dim myobj as new MyClass
myobj = jsonconvert.deserializeobject(of MyClass)(myjson)

That will turn your JSON string into an instance of MyClass called myobj, and you can access its properties directly, changing whatever you want:

myobj.MerchantReferenceCode = "SpecialMerchant"

Note that if the class definition for MyClass has properties that the JSON has no values for, they'll be present in the new class instance with no values. You can then set those values as you wish.

If the JSON has properties that don't exist in your class, they'll be dropped/lost in the conversion, so you do need to study your source data.

And you turn it back into a string again easily:

dim newjson as string = jsonconvert.serializeobject(myobj)
technonaut
  • 484
  • 3
  • 12