Home > Software design >  How do I change the property value name from an JSON file to object?
How do I change the property value name from an JSON file to object?

Time:06-29

So basically I have this json file

[
 {
"_id": "62bab08c83586a7bb36b46de",
"index": 0,
"tags": [
  "ea in minim in occaecat pariatur cillum",
  "ut exercitation minim officia enim cillum anim",
  "ad occaecat labore velit cupidatat enim proident",
  "consequat culpa qui occaecat sit sunt voluptate",
  "eiusmod excepteur adipisicing tempor ut Lorem do",
  "quis velit aliquip ad excepteur deserunt do",
  "dolor fugiat ea sit adipisicing labore in"
]
},
{
"_id": "62bab08c10365bb88f81cdf5",
"index": 1,
"tags": [
  "non laborum cillum commodo velit culpa commodo",
  "nisi aute magna laborum ut cillum velit",
  "in veniam ullamco officia aute deserunt ex",
  "dolor ullamco aliqua laborum ullamco officia mollit",
  "fugiat aliquip nostrud deserunt fugiat veniam veniam",
  "culpa eu irure ullamco ea deserunt ullamco",
  "labore quis quis enim magna duis cupidatat"
 ]
 },
 ]

I have to transfer all the data from the JSON file to my local object let localobj; And after that change the property names :

id => person_id

index => idx

tags => voiceLines;

I can't seem to grasp around the concept of changing the properties name using javascript, is it even possible?

CodePudding user response:

This would be a great application of a .map() array method in javascript, which lets you iterate over an array, and return a new array. .map() works like this:

const originalJsonArray = [...] //Whatever your original input is here

const newJsonArray = originalJsonArray.map((singleItem)=>{
   return {
      person_id: singleItem._id,
      idx: singleItem.index,
      voiceLines: singleItem.tags
   }
})

CodePudding user response:

You can iterate your data array and set the properties of your object, try this:

        let data = [
            {
            "_id": "62bab08c83586a7bb36b46de",
            "index": 0,
            "tags": [
                "ea in minim in occaecat pariatur cillum",
                "ut exercitation minim officia enim cillum anim",
                "ad occaecat labore velit cupidatat enim proident",
                "consequat culpa qui occaecat sit sunt voluptate",
                "eiusmod excepteur adipisicing tempor ut Lorem do",
                "quis velit aliquip ad excepteur deserunt do",
                "dolor fugiat ea sit adipisicing labore in"
                ]
            },
            {
            "_id": "62bab08c10365bb88f81cdf5",
            "index": 1,
            "tags": [
                "non laborum cillum commodo velit culpa commodo",
                "nisi aute magna laborum ut cillum velit",
                "in veniam ullamco officia aute deserunt ex",
                "dolor ullamco aliqua laborum ullamco officia mollit",
                "fugiat aliquip nostrud deserunt fugiat veniam veniam",
                "culpa eu irure ullamco ea deserunt ullamco",
                "labore quis quis enim magna duis cupidatat"
                ]
            },
        ];


    let local = [];

    data.forEach(item => {
        let obj = {};
        obj.person_id = item['_id'];
        obj.idx = item['index'];
        obj.voiceLines = item['tags'];
        local.push(obj);
    });

    console.log(local);

CodePudding user response:

You must use .map() to iterate over your data, since it is in an array. Notice that each new property name is on the left side and the original property names are on the right, since you are reading them from each original entry in your data array.

const modifiedData = data.map((entry) => {
  return {
    person_id : entry._id,
    idx : entry.index,
    voiceLines : entry.tags
  }
})

For more help, see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

  • Related