Home > Software engineering >  I want to receive the result in JSON format using JSON.parse, but it doesn't work
I want to receive the result in JSON format using JSON.parse, but it doesn't work

Time:09-01

The results I am currently receiving are as follows.

{
    "data": [
        {
            "id": 10,
            "name": "aa",
            "parent_id": 8,
            "products": "[{\"name\": \"aaa\", \"type\": \"aaa\", \"saleRate\": null, \"salePrice\": null, \"description\": \"aaa\"}, {\"name\": \"aaa\", \"type\": \"aaa\", \"saleRate\": null, \"salePrice\": null, \"description\": \"aaa\"}, {\"name\": \"aaa\", \"type\": \"aaa\", \"saleRate\": null, \"salePrice\": null, \"description\": \"aaa\"}]"
        }
    ]
}

But I want to get the result like below.

{
    "data": [
        {
            "id": 10,
            "name": "aa",
            "parent_id": 8,
            "products": [{
              "name": "aaa", 
              "type": "aaa", 
              "saleRate": null, 
              "salePrice": null, 
              "description": "aaa"
            },{
              "name": "aaa", 
              "type": "aaa", 
              "saleRate": null, 
              "salePrice": null, 
              "description": "aaa"
            },{
              "name": "aaa", 
              "type": "aaa", 
              "saleRate": null, 
              "salePrice": null, 
              "description": "aaa"
            }]
        }
    ]
}

I tried the following using JSON.parse().

const readCategory = async (categoryId) => {
  const categories = await categoryDao.readCategory(categoryId);
  return JSON.parse(categories);
};

But it is still unchanged. Any other solution? I want help.

CodePudding user response:

If you're trying to use JSON format, your problem is that the data within the [...] also needs to be in pairs, and grouped in {...}. For better understanding check this link.

For instance,

{ 
   "sales": [ 
         { "firstname" : "John", "lastname" : "Brown" },
         { "firstname" : "Marc", "lastname" : "Johnson" }
     ] // end of sales array
}

So you might have:

 {
     "COLUMNS": [ 
      {"REGISTRATION_DT" : "19901212", "USERNAME" : "kudos", "PASSWORD" : "tx91!#1", ... },
      {"REGISTRATION_DT" : "19940709", "USERNAME" : "jenny", "PASSWORD" : "fxuf#2", ... },
      {"REGISTRATION_DT" : "20070110", "USERNAME" : "benji12", "PASSWORD" : "rabbit19", ... }
     ]
 }

If the server is sending you something which you refer to as res, you can just do this to parse it in your Javascript:

var o=JSON.parse(res);

You can then cycle through each instance within columns like follows:

for (var i=0;i<o.COLUMNS.length;i  )
{  
        var date = o.COLUMNS[i].REGISTRATION_DT; .... 
}

Hope this helps!

CodePudding user response:

Can you once try this?

const readCategory = async (categoryId) => {
  const categories = await categoryDao.readCategory(categoryId);
  categories.map((c) => {
  c.data.forEach((d) => {
    return {
      ...d,
      products:JSON.parse(d.products)
    }
  });
  });
  return categories;
};

CodePudding user response:

Create a model or entity with exact fields as your expected response, then map your response to the entity

CodePudding user response:

You need to loop through the JSON and run the JSON.parse on each products property.

let json_str = '{}'; // JSON from API
let json_obj = {};
try {
    json_obj = JSON.parse(json_str);
} catch (e) {
}
(json_obj['data'] || []).forEach(function(element, index) {
    if (typeof element['products'] === 'string') {
        try {
            element['products'] = JSON.parse(element['products']);
        } catch (e) {
        }
    }
    json_obj['data'][index] = element;
});

CodePudding user response:

A one liner solution would be

var obj = {
    "data": [
        {
            "id": 10,
            "name": "aa",
            "parent_id": 8,
            "products": "[{\"name\": \"aaa\", \"type\": \"aaa\", \"saleRate\": null, \"salePrice\": null, \"description\": \"aaa\"}, {\"name\": \"aaa\", \"type\": \"aaa\", \"saleRate\": null, \"salePrice\": null, \"description\": \"aaa\"}, {\"name\": \"aaa\", \"type\": \"aaa\", \"saleRate\": null, \"salePrice\": null, \"description\": \"aaa\"}]"
        }
    ]
}

obj.data.forEach(d => d.products = JSON.parse(d.products))
  • Related