stackoverflow
stackoverflow

Reputation: 63

How to recreate json object

I have json object like this

[
      {
        "name": "first_name",
        "value": "sssssssssssssssssss"
      },{
        "name": "email",
        "value": "[email protected]"
      }, {
        "name": "address",
        "value": "ssssssssssssssssssss"
      }, {
        "name": "PhoneNumber",
        "value": "12342123321"
      }     
    ]

This data is coming on form subbmission

But i want json data as

{
    "formProperties": {
        "table": "users",
        "mode": "insert",
        "method":"post",
        "action":"urlhere",
        "user":"admin"
  },
    "formValues": [
      {
        "name": "first_name",
        "value": "sssssssssssssssssss"
      },{
        "name": "email",
        "value": "[email protected]"
      }, {
        "name": "address",
        "value": "ssssssssssssssssssss"
      }, {
        "name": "PhoneNumber",
        "value": "12342123321"
      }     
    ]
}

How to reconstruct JSON object. please help me friends I was strucked with this problem.

Thanks in advance

Upvotes: 0

Views: 359

Answers (1)

yannisgu
yannisgu

Reputation: 493

Assume the JSON as string is saved in the variable called json you can use the following code:

var newObject = {
    "formProperties": {
        "table": "users",
        "mode": "insert",
        "method":"post",
        "action":"urlhere",
        "user":"admin"
    },
    "formValues": JSON.parse(json)
};

var newJson = JSON.stringify(newObject);

Upvotes: 2

Related Questions