1

How do I convert a JSON array into a JSON object. For example, I have created a variable which holds a JSON array:

[{  "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}]

How do I convert it into a JSON object with entities as a JSON object which look like this:

{"Entities ":[{  "Bank Account Name": "State Bank",
        "Currency Code": "4000",
        "Deposit Date": "5/2/1794",
        "Payment Channel": "check"}]
}

Is there a way to do this? I tried Stringify and parse.

5
  • 1
    When you say what you tried, please post some code. That way we can show you exactly why your code isn't doing what you want it to. Even better, you can create a fiddle, so we can test your code. Commented Oct 14, 2016 at 8:00
  • please provide your code when posting a problem Commented Oct 14, 2016 at 8:01
  • Is this really a question ? var newObj = {Entities: oldObj} Commented Oct 14, 2016 at 8:01
  • Search and try this one getJSONObject(int index) Commented Oct 14, 2016 at 8:02
  • 1
    i see no json. JSON is a string, representing a serialized object. Commented Oct 14, 2016 at 8:02

4 Answers 4

4

var array = [{  "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}];

var obj = {"Entities" : array};

console.log(obj);

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

Comments

1
var original  = [{  "Bank Account Name": "State Bank",
"Currency Code": "4000",
"Deposit Date": "5/2/1794",
"Payment Channel": "check"}];

var newValue = JSON.stringify({Entities:[original[0]]});

console.log(newValue);
//{"Entities":[{"Bank Account Name":"State Bank","Currency Code":"4000","Deposit Date":"5/2/1794","Payment Channel":"check"}]}

2 Comments

Why [original[0]] ?
You're right, var newValue = JSON.stringify({Entities:original}); would work just as well :)
0
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
  var obj = [{  "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}];
var objNew={};
objNew.Entitys=obj;

alert(JSON.stringify(objNew))
});
</script>
</head>
<body>

</body>
</html>

Comments

0

Simply wrap it:

var array = [{  "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}];

var jsonObject = {"Entities":array};

Comments

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.