0

I have been trying to convert a data of Nested Arrays in JSON Objects to Nested JSON Objects in Typescript. This is return data from rest api as getting in typescript:

Nested array contains square brackets.

data={ "ID":29614,"Ratio":8,"UOM":"IN", "manufacturers": [{ "manufacturerName": "john", "categories": [{ "categoryName": "Beverage", "products": [{ "uid": "567", "productID": 130927, "name": "After Shocks Popping Candy 1.06"}] }] } , { "manufacturerName": "Dan", "categories": [{ "categoryName": "Organization", "products": [{ "uid": "65", "productID": 5656, "name": "After Shocks Popping Candy 2.06"}] }] } ] }


Format I need is:

data=[{ "ID":29614,"Ratio":8,"UOM":"IN", "manufacturers": { "manufacturerName": "john", "categories": { "categoryName": "Beverage", "products": { "uid": "567", "productID": 130927, "name": "After Shocks Popping Candy 1.06"} } } , { "manufacturerName": "Dan", "categories": { "categoryName": "Organization", "products": { "uid": "65", "productID": 5656, "name`enter code here`": "After Shocks Popping Candy 2.06"} } } }]

Output nested array should remove square brackets.

I have tried this as

public gridData: any[];
//gridData  for bind Kendo grid 

return this.restApi.getProductBin().subscribe((data: {}) => {
this.gridData = Array.of(data); //convert to array
}

3
  • this.gridData = typeof data === Array ? data : [data] Commented Dec 30, 2019 at 15:26
  • Kendo Grid binding UI format as: <kendo-grid [data]="gridData" [height]="150" style="float:left;"> <kendo-grid-column field="ID" title="ID" width="50"> </kendo-grid-column> <kendo-grid-column field="manufacturers.manufacturerName" title="manufacturerName" width="50"> </kendo-grid-column> </kendo-grid> Commented Dec 30, 2019 at 15:30
  • This is a horribly written question. Kindly improve. You don't need good english, you simply need good formatting. Commented Dec 30, 2019 at 15:53

2 Answers 2

1

If all you want to do is remove the square brackets, you could stringify the JSON, use a regular expression to remove all '[' and ']', and then parse it back into a JSON object:

getNoArrayJSON(data: {}): {} {
    let dataString = JSON.stringify(data);
    dataString = dataString.replace(/(\[)|(\])/g, "");
    return JSON.parse(dataString);
});

I wouldn't do this unless you know that the arrays in the response you're dealing with will only ever have a size of 1.

Working Example: RegEx Remove arrays

If you're set on putting that result in an array, let newData = [getNoArrayJSON(data)]; and the format will exactly match your desired output.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please Format below data = { "ID":29614,"Ratio":8,"UOM":"IN", "manufacturers": [{ "manufacturerName": "john", "categories": [{ "categoryName": "Beverage", "products": [{ "uid": "567", "productID": 130927, "name": "After Shocks Popping Candy 1.06"}] }] } , { "manufacturerName": "Dan", "categories": [{ "categoryName": "Organization", "products": [{ "uid": "65", "productID": 5656, "name": "After Shocks Popping Candy 2.06"}] }] } ] } @David Fagerburg
Could you please help me to format of multiple size of data @David Fagerburg
0

Try like this:

  output = [];

  constructor() {
    this.output.push({
      ID: this.data.ID,
      Ratio: this.data.Ratio,
      UOM: this.data.UOM,
      manufacturers: {
        manufacturerName: this.data.manufacturers[0].manufacturerName,
        categories: {
          categoryName: this.data.manufacturers[0].categories[0].categoryName,
          products: this.data.manufacturers[0].categories[0].products[0]
        }
      }
    });
  }

Working Demo

2 Comments

,@MEDZ ,I there any way to remove Square brackets without mention array object names to use commonly.@Adrita Sharma
Could you plz help to this JSon data loop through,To remove square brackets.@Adrita Sharma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.