0

I need to store the following JSON object in a struct.

{ 
   "id":"ea6d69b3-f1",
   "type":"ofoo",
   "channel":"website",
   "funding":[ 
      { 
         "type":"xxx",
         "amount":"1",
         "cohort":"xxx",
         "promotion":"53a16648-07"
      },
      {
        "type":"xxx",
         "amount":"1",
         "cohort":"conl",
         "promotion":"53a16648-3"
      } 
   ],
   "createdAt":"2019-11-16T01:30:28.538789516Z",
   "totalAmount":"2" // Total amount of sum of amounts in funding tag
}

I know storing it as a simple nested struct is trivial, but having to add funding.amount + funding.amount to arrive at totalamount.

So far, I have tried this:

pragma solidity ^0.5.11;
contract Analytics {
  struct Field {
    string typeOfFunding; // type is a reserved word in solidity https://solidity.readthedocs.io/en/v0.4.21/miscellaneous.html#reserved-keywords
    uint amount;
    string cohort;
    string promotion;
  }
  struct Entry {
    string typeofEntry; // lack of better terminology
    string channel;
    Field[] funding;
    string createdAt;
    uint totalAmount;
  }
  mapping(string => Entry) entries;


  function updateEntry (string memory id,
                        string memory _typeOfEntry,
                        string memory _channel,
                        Field[]  _funding,
                        string memory _createdAt) public
                        returns (bool sucess)
  {
    Entry storage e = entries[id];
    e.typeofEntry = _typeOfEntry;
    e.channel = _channel;
    // e.funding = _funding;
    // e.funding = "0";
    // e.funding.push(_funding);
    e.createdAt = _createdAt;
    // e.totalAmount = updateFunding(_funding);
    return true;
  }

  function updateFunding(string memory id,  Field[]  _fundingP) private view returns (Field[] _funding)
  {
    Field[] memory funding;
    Field[] memory amount;

    for _fundingP.amount 
    {
      funding.push(_funding.amount)
    }
  }

I would appreciate pointers on this issue

2
  • You might wanna consider rephrasing your question in order to make it more readable. I, for one, have read it several times and "got lost" somewhere along the way. Why is "having to add funding.amount + funding.amount to arrive at totalamount" a problem (i.e., since when did adding two values become a problem)? Furthermore, there is no mentioning of any other problem, just a piece of code which doesn't even seem fully related to the initial description (e.g., why do you need a mapping in order to represent a JSON structure?), followed by "I would appreciate pointers on this issue". Feb 11, 2020 at 10:19
  • Thanks!! will do ASAP @goodvibration
    – 0xsegfault
    Feb 11, 2020 at 10:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.