Solidity: Storing JSON data. Any advantage in using a Struct type vs a String?

Viewed 3647

I must store JSON formatted data in my Solidity contract. I don't need to do any operations on the data. I simply need to store it, update it, and return it.

Let's say I have JSON formatted data such as:

{'name': 'Nike', 'size':'12', 'color':'blue'}

I'm currently passing the data to the constructor as a string:

constructor(string _data) public {
  data = _data;
}

And updating the data by simply replacing the entire string:

function updateData(string _data) public {
  data = _data;
}

I'm debating whether I should create a Struct type, named say "Shoe", and pass each property as an argument:

constructor(string _name, uint size, string _color) public {
  Shoe memory newShoe = Shoe({
    name: _name,
    size: _size,
    color: _color
  })

  data = newShoe;
}

I'll never need to store more than one shoe object, and it seems much simpler and easier to pass the data as a String, but I'm wondering if there's an advantage to using a Struct type.

1 Answers

Passing data as a string seems more appropriate since you don't need to manipulate them on the contract. It will be easier and less prone to mistakes and bugs. It will also be cheaper in terms of gas, that is if you are concerned about gas anyway.

Related