0

I have stored my data on firebase, and taken as a JSON file, the JSON should be separated and display into the webpage through the Ethereum blockchain, for example:

{
  product: "Shoes",
  prize: "85",
}

I want to display the above JSON in the website through the blockchain

  • 1
    JSON and string is not an efficient way to store data in Ethereum. Consider encoding/hasing data/data structures offchain – Igor Barinov Jan 12 at 16:58
0

There are lots of different ways you could manage writing this data to ethereum. Depending on how big your JSON files are / how many json files you'll need to publish, this is a potential solution.

Deploy the contract below and transact with the writeJsonFile to emit JSONFileEvents from this smart contract, these events will be stored in the blockchain and can be queried to be displayed on a website.

pragma solidity ^0.5.0;

contract JsonFile {

    event JSONFileEvent(uint256 id, bytes32 product, uint256 prize);

    function writeJsonFile(uint256 _id, bytes32 _product, uint256 _prize) public {
        emit JSONFileEvent(_id, _product, _prize);
    }
}

This is a very basic example - you could introduce additional logic to actually store values in the smart contract if you need. You could also introduce logic to make sure the ID's are unique.


Example View the example below - given we are using bytes32 for the string - you will likely need to pad the input to be 32 bytes. As seen in the example below and acceptable input is is: "0x73686f6573000000000000000000000000000000000000000000000000000000" which is 'shoes' converted to hex and padded with zeros. If you are using remix, it doesn't seem to do the implicit conversion from a sting to bytes32

enter image description here

  • how it produces the ouput – Navin M Jan 13 at 14:54
  • it shows error encoding arguments – Navin M Jan 13 at 14:55
  • You probably need to send in the bytes32 version of the string - view updated answer – Steven V Jan 13 at 16:14

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.