skip to Main Content

I have a JSON file that is taken from the data of the project. However, there are information in it that I do not need for my specific goal. This is the JSON data:

const toSql = [
    
    {"id":1,"battlerName":"Actor1_1","characterIndex":0,"characterName":"Actor1","classId":1,"equips":[2,0,0,3,0],"faceIndex":0,"faceName":"Actor1","traits":[{"code":51,"dataId":2,"value":1},{"code":51,"dataId":4,"value":1}],"initialLevel":1,"maxLevel":99,"name":"Reid","nickname":"","note":"","profile":""},
    {"id":2,"battlerName":"Actor1_2","characterIndex":1,"characterName":"Actor1","classId":1,"equips":[1,0,0,9,0],"faceIndex":1,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Priscilla","nickname":"","note":"","profile":""},
    {"id":3,"battlerName":"Actor1_3","characterIndex":2,"characterName":"Actor1","classId":5,"equips":[31,0,65,5,0],"faceIndex":2,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Gale","nickname":"","note":"","profile":""},
    {"id":4,"battlerName":"Actor1_4","characterIndex":3,"characterName":"Actor1","classId":5,"equips":[31,0,0,5,0],"faceIndex":3,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Michelle","nickname":"","note":"","profile":""},
    {"id":5,"battlerName":"Actor1_5","characterIndex":4,"characterName":"Actor1","classId":2,"equips":[7,0,0,20,0],"faceIndex":4,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Albert","nickname":"","note":"","profile":""},
    {"id":6,"battlerName":"Actor1_6","characterIndex":5,"characterName":"Actor1","classId":2,"equips":[7,0,65,21,0],"faceIndex":5,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Kasey","nickname":"","note":"","profile":""},
    {"id":7,"battlerName":"Actor1_7","characterIndex":6,"characterName":"Actor1","classId":3,"equips":[7,0,0,20,0],"faceIndex":6,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Eliot","nickname":"","note":"","profile":""},
    {"id":8,"battlerName":"Actor1_8","characterIndex":7,"characterName":"Actor1","classId":3,"equips":[7,0,0,20,0],"faceIndex":7,"faceName":"Actor1","traits":[],"initialLevel":1,"maxLevel":99,"name":"Roza","nickname":"","note":"","profile":""}
    ]

However, I just need the following: id and traits

How would I go around extracting those two only and make it as another json file?

2

Answers


  1. That’s not JSON, that’s an array of object literals. You can use the map function to loop through each item in the array and convert them to a new object.

    toSql.map(b => ({ id: b.id, traits: b.traits }));
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

    Login or Signup to reply.
  2. In JavaScript even though your data has a similar structure to a JSON, it is not, it is an Array of Objects.

    In this case, you want to extract X amount of fields from the original one. The way to do this, is to return the new value of the object we want it to be. item is each entry of the array.

    Let’s say we want to extract the fields "battlerName" and "id". We return them inside an object inside of the map function. The map function will iterate the entire array and will modify each entry to suit the condition you added.

    Example:

    toSql.map((item) =>{
      //We just want to return the name "id" and "battlerName".
      return {
        "id": item.id,
        "battlerName": item.battlerName
      }
    })
    

    Learn More Here:
    https://www.w3schools.com/jsref/jsref_map.asp

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search