0

My app is reading in multiple Json files in Typescript populate select boxes, but I want to avoid wet (write everything twice) code and keep things dry (don't repeat yourself). I originally had unique functions for each json file, as they each have different values and need to be parsed differently, but I instead one to make one function that can be called from my main class that will do the reading and parsing into an array and return.

For example, my Countries Json looks like this:

{
  "Countries": {
    "country": [
      {
        "_CountrySEQ": "0",
        "_CountryCodeDesc": "UNITED STATES",
        "_CountryCode": "USA"
      },
      {
        "_CountrySEQ": "1",
        "_CountryCodeDesc": "CANADA",
        "_CountryCode": "CAN"
      },
      , , ,
    }
}

I would need it to be parsed into a key/value array and looks like this:

[
    {
        key:'USA',
        value:'UNITED STATES'
    },
    {
        key:'CAN',
        value:'CANADA'
    }
    . . .
]

But I have a statesjson that looks like this:

    {
      "States": {
        "state": [
          {
            "_State": "(cantons) Aargau",
            "_CountryCode": "CHE"
          },
          {
            "_State": "Abruzzi",
            "_CountryCode": "ITA"
          }
          . . .
       ]
     }
   }

And needs to be parsed out to look like this:

[
    {
        key:'CHE',
        value:'(cantons) Aargau'
    },
    {
        key:'ITA',
        value:'Abruzzi'
    }
    . . .
]

Is there a way to do this a modular as possible? Like a single function to handle this? Are arrays of classes or interfaces the best solution? And if so, how do I properly implement them?

MARS
  • 499
  • 1
  • 9
  • 25

2 Answers2

1

This function handles both datasets.


const parser = (dataset) => {
  const [first, second, index1, index2] = 
    dataset.Countries ? 
    ["Countries", "country", 2, 1] : ["States", "state", 0, 1];
  return dataset[first][second].map(item => {
    return {
      key1: Object.values(item)[index1],
      key2: Object.values(item)[index2],
    }
  });
};

parser(Countries);
parser(States);

  • Thank you! This is a huge help! I have 7 json files I need to get this to work with, so what I'll do is put this in a class and whenever I call it, I'll pass in the unique identifiers (where you have `"Countries", "country", etc`). That way, It will be just one function that is doing the reading and parsing and all I have to do is just tell it where the file is and give it the ids. Again, thanks a ton! – MARS May 06 '20 at 21:16
  • 1
    Well i am happy to find a solution. If you are planning to pass different type of datasets, i think you can improve the part where i create `first, last, index1, index2`. I am sure you can reduce the argument quantity of the function. Well... have a good one! –  May 06 '20 at 21:19
0

Having two functions in your case is not DRY at all. Keep it simple, start thinking of another solution if you have 2 more json. E.g

function parseJson1(json) {
  return json.Countries.country.map(v => ({key: v._CountryCode, value: v._CountryCodeDesc}));
}

function parseJson2(json) {
  return json.States.state.map(v => ({key: v._CountryCode, value: v._State}));
}

One function for each json, each function only has 1 line. Why would you try to make it more complex?

HTN
  • 3,388
  • 1
  • 8
  • 18