Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial help to change structure of your json to array, many times we get unstructured data from api response then we have to do re structure data in react native.

Today, we are going to learn how we can change schema of json data and also how we can convert json data to array.

First start from, how we can change json key name in react native.

Suppose, You have json data with key name id, and name but you want to change id => key and name => label.

Let’s see how we can do that…

Coming JSON from something like,

 [
    {
      "id": 1,
      "name": "Hotels",
    },
    {
      "id": 2,
      "name": "Embassies",
    },
 ]

But, you want to change key name like below

[
    {
      key: 1,
      label: "Hotels",
    },
    {
      key 2,
      label: "Embassies",
    },
 ]

Convert JSON key example

var jsonData = [
    { "id": 1, "name": "Hotels" },
    { "id": 2, "name": "Embassies" }
  ];

var data = jsonData.map(function(item) {
  return {
    key: item.id,
    label: item.name
  };
});

console.log(data);

console.log(data); should so your desired output, using above example map we are understand how we can change json to our wanted structure also…

Thanks For Reading…