0

I am converting array of hash into a string then store it in the smart contract. It is throwing error as;

transact to Test.set errored: Error encoding arguments: SyntaxError: Unexpected number in JSON at position 46

Array: [0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c,0xca35b7d915458ef540ade6068dfe2f44e8fa733c]

It is working fine when length of array is 2, more than this it is not working.

pragma solidity ^0.4.24;

contract Test {
   string public t;
   function set(string _t) public {
       t = _t;
   }
}

Conversion from array to string is handle by a separate javascript.

1

Not sure exactly where you are going wrong since you do not share the JavaScript which converts the array to a string, but in theory what you are doing should be just fine.

I recommend that you use the JSON.stringify() function to convert your array into a proper string.

>> let arr = new Array(10).fill("0xca35b7d915458ef540ade6068dfe2f44e8fa733c")

>> arr
Array(10) [ "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c", "0xca35b7d915458ef540ade6068dfe2f44e8fa733c" ]

>> JSON.stringify(arr)
"[\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\",\"0xca35b7d915458ef540ade6068dfe2f44e8fa733c\"]"

This last string should have no such syntax errors. To undo the stringify, just use JSON.parse().

  • Thank you it works. Actually I was using toString() for conversion. Don't know what exactly is the difference between 2. – Shubham Chadokar Nov 23 '18 at 7:13
  • Thank you the string in your it works. How it is adding '\' ? When I am doing JSON.stringify() it is just making it in string format, no '\' were there and without those '\' it is giving the same error. – Shubham Chadokar Nov 23 '18 at 7:26
  • The backslash is an "escape character" telling the JavaScript to parse the following character as a character, rather than code syntax. This is how you can use quotation marks within a string. JSON.stringify and JSON.parse are standard in JavaScript, and should pretty much always be your go to when doing things like this. – Shawn Tabrizi Nov 23 '18 at 11:11

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.