1

I am looking for a solution to concat double quotes ("") into a string in Solidity.

What I need looks like this:

'{"id":idHere,"Lname":lname,"Fname":fname}'

All I get is sth like '{\"id\":idHere,\"Lname\":lname,\"Fname\":fname}'

PS: idHere, lname and fname are variables not constant value.

  • 2
    This isn't quite clear to me. Can you share input and expected output? – Shawn Tabrizi Oct 24 '18 at 7:45
  • Do you mean to use an escape character? string double_quoted_stuff = "\"Interesting\""; // contents: "Interesting" – Henk Oct 24 '18 at 8:00
  • Are you thinking of doing "JSON stuff" in your solidity code? There's no such thing as "JSON" in solidity, if this is indeed your intention. The mapping type, however, sounds just about right for the job (but again, this is a mere speculation, since it is not clear what you're actually trying to achieve). – goodvibration Oct 24 '18 at 8:13
  • What i want to do here is to do an oraclize_query("URL", "example.com",'{"id":idHere, "Lname":lname, "Fname":fname}') as a HTTP POST to the API. For this purpose i need to have a string concat with the whole json string – Sienz Pz Oct 24 '18 at 8:29
1

There is a strConcat() function in the usingOraclize contract that you are most likely inheriting from. You could use it like this:

  function queryString(
    uint256 _dynamicVariable
  ) 
    internal
    returns (string)
  {
    return strConcat(
      apiPrefix, // https:// or something like that
      encryptedApiComponent, // my example encrypts the domain
      "}", // closing part of oraclize query
      uint2str(_dynamicVariable), // also from oraclize 
      apiSuffix
    );
  }
  • It is worth noting that " (0x22 in hex) is just another character which is encoded into bytes... the way solidity handles strings can be pretty low level... almost any operation you are going to do on strings involves casting the string to bytes and working on the actual bytes that represent the characters in a string. On a low level what you are going to be doing is looping through the bytes array and looking for 0x22. You can then do what you want at that point... – TovarishFin Oct 24 '18 at 10:10
  • also... this article I wrote a while back might give you some sort of an idea on what is happening with strings... even if the main objective of the article is different. medium.codylamson.com/… – TovarishFin Oct 24 '18 at 10:12
  • can you give exact code on how to get the json on the above as i mentioned? thank you – Sienz Pz Oct 24 '18 at 17:29
1

With the help of community, i can achieve my purpose with this:

string memory str1 = strConcat('{','"id":',ids,',','"lname":');

string memory str2 = strConcat('"',lname,'"',',','"fname":');

string memory str3 = strConcat('"',fname,'"','}');

string memory str4 = strConcat(str1,str2,str3);

Although it seems tideous but it solves my problem. Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.