0

I have a set of key-value pairs in which there are url's of images and this json file is saved in the realtime database of firebase.My question is can we reorder the values(url's) after exporting the json in desktop and opening in notepad++? or is there any other way?

If we can reorder the values like in this example the alone pair have 3 url's and if we can reorder them such a way that the first one becomes third and second one becomes first then the end user on the application can see the photo in different order every time we change/reorder/shuffle url's in database. As there are only three url's given for alone it's easy for us and we can do it manually and it's not time consuming, just imagine that the alone pair is having 100 url's then it's difficult to change manually each url as it's time consuming and a lot more confusing

An example of json file is-

{
  "URLs" : {
    "ALONE" : [ "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/alone.jpg?alt=media&token=1f8ee2bd-7bd2-4223-b708-2ae48ab3c5da", "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/6094594.jpg?alt=media&token=2c2611af-6e15-4d8e-8d4e-32fa347d8025", "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/8620007.jpg?alt=media&token=c54ec2ff-8c15-41ba-b0f1-e9d256586ce2",],
    "AMAZING" : [ "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/amazing.jpg?alt=media&token=381900b3-cab9-4802-a9db-6d8b938d3d16", "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/7514001.jpg?alt=media&token=1e424124-103f-4bbe-b28d-736896d3afe4", "https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/4546211.jpg?alt=media&token=35fbf774-4262-4c45-8e7d-8efbaafabd29",],
  }
}

P.S.- This is just an example file so only a few is given the actual file is too big to just shuffle it manually in notepad++

2 Answers2

0

The order of keys in a JSON object is by definition undefined. So while you can reorder the lines in the text file that you export, that does not necessarily affect the JSON object when the file is parsed again.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

I understand that you want to shuffle the arrays in order to display the pictures in a different order each time.

You will find in this SO question different methods to shuffle an array.


The following code will shuffle each array of the URLs object.

  function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  }

  const rawObj = {
    URLs: {
      ALONE: [
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/alone.jpg?alt=media&token=1f8ee2bd-7bd2-4223-b708-2ae48ab3c5da',
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/6094594.jpg?alt=media&token=2c2611af-6e15-4d8e-8d4e-32fa347d8025',
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/8620007.jpg?alt=media&token=c54ec2ff-8c15-41ba-b0f1-e9d256586ce2'
      ],
      AMAZING: [
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/amazing.jpg?alt=media&token=381900b3-cab9-4802-a9db-6d8b938d3d16',
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/7514001.jpg?alt=media&token=1e424124-103f-4bbe-b28d-736896d3afe4',
        'https://firebasestorage.googleapis.com/v0/b/simple-motivation.appspot.com/o/4546211.jpg?alt=media&token=35fbf774-4262-4c45-8e7d-8efbaafabd29'
      ]
    }
  };

  const urlsObj = rawObj.URLs;
  const keys = Object.keys(urlsObj);
  for (const key of keys) {
    console.log(urlsObj[key]);
    console.log(shuffleArray(urlsObj[key]));
  }

If you want the shuffling to be applied to all the elements of all the arrays, you could do as follows:

  function shuffleArray(array) {
    //.....
    return array;
  }

  const rawObj = {...};

  const urlsObj = rawObj.URLs;
  const keys = Object.keys(urlsObj);

  let fullArray = [];
  for (const key of keys) {
    fullArray = fullArray.concat(urlsObj[key]);
  }
  console.log(fullArray);
  console.log(shuffleArray(fullArray));
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121