Home > Enterprise >  How to create a CSV File that will look Like This JSON File
How to create a CSV File that will look Like This JSON File

Time:06-02

I am basically wanting to update multiple scholars for an NFT game (axie infinity). It requires a JSON file that looks like this:

{
        "name": "Scholar 1",
        "ronin": "ronin:<account_s1_address>",
        "splits": [
            {
                "persona": "Manager",
                "percentage": 44,
                "ronin": "ronin:<manager_address>"
            },
            {
                "persona": "Scholar",
                "percentage": 40,
                "ronin": "ronin:<scholar_1_address>"
            },
            {
                "persona": "Other Person",
                "percentage": 6,
                "ronin": "ronin:<other_person_address>"
            },
            {
                "persona": "Trainer",
                "percentage": 10,
                "ronin": "ronin:<trainer_address>"
            }
        ]
    }

But since there are multiple scholars/players, I wanted to know if there was anyway to format something on a CSV file that if I convert or import it using a JSON tool it will look like like the JSON above?

Your help is much appreciated.. Thank you!

PS:

The first lines:

"name": "Scholar 1",
            "ronin": "ronin:<account_s1_address>",
            "splits":

Would need to be repeated since again there are multiple scholars, i.e. Scholar 1, Scholar 2, Scholar 3...

CodePudding user response:

CSV file structure is column-base, if Axie infinity require JSON file, you can create a CSV file by Excel or Google sheet and convert to JSON.

there is a similar answer to convert CSV to JSON

CodePudding user response:

starting from this CSV that has this structure

name ronin id_persona persona percentage split_ronin
Scholar 1 ronin:<account_s1_address> 1 Manager 44 ronin:<manager_address>
Scholar 1 ronin:<account_s1_address> 2 Scholar 40 ronin:<scholar_1_address>
Scholar 1 ronin:<account_s1_address> 3 Other Person 6 ronin:<other_person_address>
Scholar 1 ronin:<account_s1_address> 4 Trainer 10 ronin:<trainer_address>

you can run this Miller command

mlr --c2j reshape -r "^(p|s)" -o k,v  then \
put '$k="splits".".".${id_persona}.".".$k' then \
cut -x -f id_persona then \
reshape -s k,v out.csv

to have

[
  {
    "name": "Scholar 1",
    "ronin": "ronin:<account_s1_address>",
    "splits": [
      {
        "persona": "Manager",
        "percentage": 44,
        "split_ronin": "ronin:<manager_address>"
      },
      {
        "persona": "Scholar",
        "percentage": 40,
        "split_ronin": "ronin:<scholar_1_address>"
      },
      {
        "persona": "Other Person",
        "percentage": 6,
        "split_ronin": "ronin:<other_person_address>"
      },
      {
        "persona": "Trainer",
        "percentage": 10,
        "split_ronin": "ronin:<trainer_address>"
      }
    ]
  }
]

Some notes:

  • reshape -r "^(p|s)" -o k,v, to transform the input from wide to long;
  • put '$k="splits".".".${id_persona}.".".$k', to create values that I will use as field names (splits.1.persona,splits.1.percentage,splits.1.split_ronin,splits.2.persona,splits.2.percentage, ....
  • cut -x -f id_persona, to remove the field id_persona;
  • reshape -s k,v, to transform all from long to wide.

The real goal is to build, starting from that input, this kind of CSV

 ----------- ---------------------------- ------------------ --------------------- ------------------------- ------------------ --------------------- --------------------------- ------------------ --------------------- ------------------------------ ------------------ --------------------- ------------------------- 
| name      | ronin                      | splits.1.persona | splits.1.percentage | splits.1.split_ronin    | splits.2.persona | splits.2.percentage | splits.2.split_ronin      | splits.3.persona | splits.3.percentage | splits.3.split_ronin         | splits.4.persona | splits.4.percentage | splits.4.split_ronin    |
 ----------- ---------------------------- ------------------ --------------------- ------------------------- ------------------ --------------------- --------------------------- ------------------ --------------------- ------------------------------ ------------------ --------------------- ------------------------- 
| Scholar 1 | ronin:<account_s1_address> | Manager          | 44                  | ronin:<manager_address> | Scholar          | 40                  | ronin:<scholar_1_address> | Other Person     | 6                   | ronin:<other_person_address> | Trainer          | 10                  | ronin:<trainer_address> |
 ----------- ---------------------------- ------------------ --------------------- ------------------------- ------------------ --------------------- --------------------------- ------------------ --------------------- ------------------------------ ------------------ --------------------- ------------------------- 

and than use it to create the final JSON output

  • Related