0

I am facing issues while constructing an object using javascript. I want this:

{
    "p_id": "2",
    "p_name": "weblogic",
    "ip_list": [
        {
            "ip_id": 2690
        },
        {
            "ip_id": 2692
        },
        {
            "ip_id": 2693
        }
    ]
}

Below is the javascript code that I am using to get the data into the object:

var ipArray = [];
secTagJSON.p_name = "weblogic";
secTagJSON.p_id = "2";
for (var index=0; index < selectedArray.length; index++){
    secTagJSON.ip_list.push("ip_id": selectedArray[index]);
}

I am able to construct the properties for p_id and p_name but struggling to create the the ip_list. Please let me know how to get this constructed using javascript.

Code for posting to the server:

var ipArray = [];
secTagJSON.p_name = "weblogic";
secTagJSON.p_id = 2;
for (var index=0; index < selectedArray.length; index++){
 secTagJSON.ip_list.push({"ip_id": selectedArray[index]});
}
console.log (secTagJSON);
console.log (JSON.stringify(secTagJSON));
 $http.post("http://server:port/api/v1/tags").
    success(function(data) {
      console.log (data)
    });
9
  • What you just posted is a valid way to construct it using Javascript. Commented Mar 27, 2014 at 18:02
  • Hmmm . What you mean by constructing ? ... because i suspect that something is terribly wrong by your words Commented Mar 27, 2014 at 18:03
  • 1
    How are you creating the json? Commented Mar 27, 2014 at 18:03
  • Please find the below code that I have written to construct the json message: var ipArray = []; secTagJSON.p_name = "weblogic"; secTagJSON.p_id = "2"; for (var index=0; index < selectedArray.length; index++){ secTagJSON.ip_list.push("ip_id": selectedArray[index]); } But I am not getting in the desired format as mentioned in my question Commented Mar 27, 2014 at 18:05
  • 1
    @JAAulde made a simple but important observation. Commented Mar 27, 2014 at 18:21

2 Answers 2

1

Simply do this:

var obj = { ip_list: [] };

obj.p_name = "weblogic";
obj.p_id = "2";

for (var i = 0, j = selectedArray.length; i < j; i++)
   obj.ip_list.push({ ip_id: selectedArray[i] });

Note that your ip_list is actually an array of objects. So, when you iterate over it, remember that each var item = json.ip_list[i] will return an object, that you can access its properties using: item['ip_id'].

Note that obj is an Javascript object, it is not an JSON. If you want the JSON, you can use JSON.stringify(obj). This will return your JSON (string).

Hope I've helped.

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

2 Comments

Yes. I got it working now but when I am trying to post the json to the server, I am getting 415 (Unsupported Media Type) error. Could you please let me know what I am missing?
How are you sending it? Did you set the contentType? And if you did, is your server expecting JSON format? I edited my answer telling you that if you wnat to send the JSON, you need to serialize your javascript object.
1

Try:

secTagJSON.p_name = "weblogic";
secTagJSON.p_id = "2";
secTagJSON.ip_list = [];
for (var index=0; index < selectedArray.length; index++){
 secTagJSON.ip_list.push({"ip_id": selectedArray[index]});
}

you forgot your {} around "ip_id": etc... You also need to declare that ip_list is an array. Your ip_list is an array of objects. I would guess that your script was not running as it was.

Posting to your server you should use:

$http.post('server:port/api/v1/tags', secTagJSON).sucess(...

3 Comments

Thanks. It worked but when I am trying to post the json I am getting 415 error - 415 (Unsupported Media Type). Can you please let me know where I am going wrong?
I have added the code to post to the server in my question. I am using AngularJs for http post calls.
@user883561 It looks like you're not actually sending the data... so the content type is not getting set by i'm guessing Angular... try $http.post('server:port/api/v1/tags', secTagJSON).sucess(...

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.