Tengo una matriz json Quiero eliminar la matriz nula de mi json.

{
  "tblFormQuestions": [
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 308,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QShortText",
      "Question": "What's your name?",
      "PlaceHolderText": "Placeholder",
      "IsNumericOnly": true,
      "CharacterLimit": 0,
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [
        {
          "ID": 112,
          "FormQuestionID": 309,
          "OptionValue": "1 "
        },
        {
          "ID": 113,
          "FormQuestionID": 309,
          "OptionValue": " 2 "
        },
        {
          "ID": 114,
          "FormQuestionID": 309,
          "OptionValue": " 3"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 309,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ1",
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [
        {
          "ID": 115,
          "FormQuestionID": 310,
          "OptionValue": "4 "
        },
        {
          "ID": 116,
          "FormQuestionID": 310,
          "OptionValue": " 5"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 310,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ2",
      "IsRequired": true
    }
  ],
  "ID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
  "Title": "",
  "CreatedTime": "2019-05-08T16:38:56.637",
  "CreatedBy": "Admin"
}

Ahora quiero eliminar toda la matriz nula [] de mi json, la nueva json debería ser así. ¿Cómo puedo hacerlo en jquery?

{
  "tblFormQuestions": [
    {     
      "ID": 308,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QShortText",
      "Question": "What's your name?",
      "PlaceHolderText": "Placeholder",
      "IsNumericOnly": true,
      "CharacterLimit": 0,
      "IsRequired": false
    },
    {     
      "tblFormQuestionDropdownOptions": [
        {
          "ID": 112,
          "FormQuestionID": 309,
          "OptionValue": "1 "
        },
        {
          "ID": 113,
          "FormQuestionID": 309,
          "OptionValue": " 2 "
        },
        {
          "ID": 114,
          "FormQuestionID": 309,
          "OptionValue": " 3"
        }
      ],
      "ID": 309,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ1",
      "IsRequired": false
    },
    {
      "tblFormQuestionDropdownOptions": [
        {
          "ID": 115,
          "FormQuestionID": 310,
          "OptionValue": "4 "
        },
        {
          "ID": 116,
          "FormQuestionID": 310,
          "OptionValue": " 5"
        }
      ],
      "ID": 310,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ2",
      "IsRequired": true
    }
  ],
  "ID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
  "Title": "",
  "CreatedTime": "2019-05-08T16:38:56.637",
  "CreatedBy": "Admin"
}
-3
Umar Asif 10 may. 2019 a las 08:29

3 respuestas

La mejor respuesta

Esto funciona

let data = {
  "tblFormQuestions": [{
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 308,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QShortText",
      "Question": "What's your name?",
      "PlaceHolderText": "Placeholder",
      "IsNumericOnly": true,
      "CharacterLimit": 0,
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [{
          "ID": 112,
          "FormQuestionID": 309,
          "OptionValue": "1 "
        },
        {
          "ID": 113,
          "FormQuestionID": 309,
          "OptionValue": " 2 "
        },
        {
          "ID": 114,
          "FormQuestionID": 309,
          "OptionValue": " 3"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 309,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ1",
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [{
          "ID": 115,
          "FormQuestionID": 310,
          "OptionValue": "4 "
        },
        {
          "ID": 116,
          "FormQuestionID": 310,
          "OptionValue": " 5"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 310,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ2",
      "IsRequired": true
    }
  ],
  "ID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
  "Title": "",
  "CreatedTime": "2019-05-08T16:38:56.637",
  "CreatedBy": "Admin"
}

let result = data.tblFormQuestions.forEach(el => {

  Object.keys(el).forEach(inner => {
    // this check if the field is array ,so we not delete the empty string fields
    if (Array.isArray(el[inner]) && el[inner].length == 0) {
      delete el[inner]
    }
    
  })
})

console.log(data);
1
adiga 10 may. 2019 a las 08:22

Recorra el objeto y, si el valor de cualquier clave es un objeto, utilice filter para eliminar la matriz vacía.

let obj = {
  "tblFormQuestions": [{
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 308,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QShortText",
      "Question": "What's your name?",
      "PlaceHolderText": "Placeholder",
      "IsNumericOnly": true,
      "CharacterLimit": 0,
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [{
          "ID": 112,
          "FormQuestionID": 309,
          "OptionValue": "1 "
        },
        {
          "ID": 113,
          "FormQuestionID": 309,
          "OptionValue": " 2 "
        },
        {
          "ID": 114,
          "FormQuestionID": 309,
          "OptionValue": " 3"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 309,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ1",
      "IsRequired": false
    },
    {
      "tblFormQuestionAddresses": [],
      "tblFormQuestionDatePickers": [],
      "tblFormQuestionDropdownOptions": [{
          "ID": 115,
          "FormQuestionID": 310,
          "OptionValue": "4 "
        },
        {
          "ID": 116,
          "FormQuestionID": 310,
          "OptionValue": " 5"
        }
      ],
      "tblFormQuestionImages": [],
      "tblFormQuestionSliders": [],
      "tblFormQuestionToggles": [],
      "ID": 310,
      "FormID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
      "StepOrder": 1,
      "QuestionType": "QDropdown",
      "Question": "DQ2",
      "IsRequired": true
    }
  ],
  "ID": "51231cc6-b7b4-44f8-a33a-684bc413ef1c",
  "Title": "",
  "CreatedTime": "2019-05-08T16:38:56.637",
  "CreatedBy": "Admin"
}
let newObj = {};
for (let keys in obj) {
  if (Array.isArray(obj[keys])) {
    newObj[keys] = obj[keys].filter(function(item) {
      return item.lenght !== 0;
    })

  }
  newObj[keys] = obj[keys]
}
console.log(newObj)
0
brk 10 may. 2019 a las 05:56

Utilizando JSON.parse

let json = `{"tblFormQuestions":[{"tblFormQuestionAddresses":[],"tblFormQuestionDatePickers":[],"tblFormQuestionDropdownOptions":[],"tblFormQuestionImages":[],"tblFormQuestionSliders":[],"tblFormQuestionToggles":[],"ID":308,"FormID":"51231cc6-b7b4-44f8-a33a-684bc413ef1c","StepOrder":1,"QuestionType":"QShortText","Question":"What's your name?","PlaceHolderText":"Placeholder","IsNumericOnly":true,"CharacterLimit":0,"IsRequired":false},{"tblFormQuestionAddresses":[],"tblFormQuestionDatePickers":[],"tblFormQuestionDropdownOptions":[{"ID":112,"FormQuestionID":309,"OptionValue":"1 "},{"ID":113,"FormQuestionID":309,"OptionValue":" 2 "},{"ID":114,"FormQuestionID":309,"OptionValue":" 3"}],"tblFormQuestionImages":[],"tblFormQuestionSliders":[],"tblFormQuestionToggles":[],"ID":309,"FormID":"51231cc6-b7b4-44f8-a33a-684bc413ef1c","StepOrder":1,"QuestionType":"QDropdown","Question":"DQ1","IsRequired":false},{"tblFormQuestionAddresses":[],"tblFormQuestionDatePickers":[],"tblFormQuestionDropdownOptions":[{"ID":115,"FormQuestionID":310,"OptionValue":"4 "},{"ID":116,"FormQuestionID":310,"OptionValue":" 5"}],"tblFormQuestionImages":[],"tblFormQuestionSliders":[],"tblFormQuestionToggles":[],"ID":310,"FormID":"51231cc6-b7b4-44f8-a33a-684bc413ef1c","StepOrder":1,"QuestionType":"QDropdown","Question":"DQ2","IsRequired":true}],"ID":"51231cc6-b7b4-44f8-a33a-684bc413ef1c","Title":"","CreatedTime":"2019-05-08T16:38:56.637","CreatedBy":"Admin"}`

let parsed = JSON.parse(json, (key, value) =>
  Array.isArray(value) 
    ? (value.length) ? value : undefined 
    : value
)

console.log(parsed)
3
User863 10 may. 2019 a las 05:51