0

Here's what I have...

The well formed JSON array

[
    {"id":"1","networkType":1,"controllerIp":"10.255.135.22","redirectType":0,"networkCost":1351,"networkWeight":64888,"notes":"This is a network for network 1","name":"n1"},
    {"id":"4","networkType":1,"controllerIp":"10.255.150.24","redirectType":0,"networkCost":1344,"networkWeight":745,"notes":"This is a network for network 4","name":"n4"},
    {"id":"3","networkType":2,"controllerIp":"10.255.150.22","redirectType":0,"networkCost":13,"networkWeight":6888,"notes":"This is a network for network 3","name":"n3"},
    {"id":"2","networkType":3,"controllerIp":"10.255.654.22","redirectType":0,"networkCost":13,"networkWeight":6888,"notes":"This is a network for network 2","name":"n2"}
]

Now I want to tack on two new elements to each part of this array...

I'm using:

    function addPrefix(rawJSON){

        var orgStr = rawJSON;

            //Loop through all the groups of .... networks, sites, resources, components
            $.each(myData1,function(key,val){
                /*
                    key would be key1,key2,key3
                    innerjson would be the name and value **
                */

                //Alerts and logging of the variable. VAL is the entire group {...}
                console.log(val); //should show you the value   
                //Now append the following to the groups...
                //"data" : val.name,
                //"metadata" : {"id" : val.id},


                alert(val.name); //Should say n1,n2,n3...nn
            });

            return orgStr;

    }

So the new array would look like this:

[
    {"data" : "1", "metadata" : {"id" : "n1"}, "id":"1","networkType":1,"controllerIp":"10.255.135.22","redirectType":0,"networkCost":1351,"networkWeight":64888,"notes":"This is a network for network 1","name":"n1"},
    {"data" : "4", "metadata" : {"id" : "n4"}, "id":"4","networkType":1,"controllerIp":"10.255.150.24","redirectType":0,"networkCost":1344,"networkWeight":745,"notes":"This is a network for network 4","name":"n4"},
    {"data" : "2", "metadata" : {"id" : "n2"}, "id":"3","networkType":2,"controllerIp":"10.255.150.22","redirectType":0,"networkCost":13,"networkWeight":6888,"notes":"This is a network for network 3","name":"n3"},
    {"data" : "3", "metadata" : {"id" : "n3"}, "id":"2","networkType":3,"controllerIp":"10.255.654.22","redirectType":0,"networkCost":13,"networkWeight":6888,"notes":"This is a network for network 2","name":"n2"}
]

How is that done??? or can it even be done?

3
  • FYI, your question has nothing to do with Json. Commented Nov 5, 2013 at 13:26
  • Yes it has EVERYTING to do with JSON... MYDATA1 is just a variable that holds the JSON that I posted above in the top part of the question. It's PASSING the DATA variable from : function(data), get it??? Into a function that I also pasted in the code above... that little teeny weeny function is going to help me add stuff to the JSON ARRAY... Commented Nov 5, 2013 at 13:32
  • So myData1 holds the JSON array, not rawJSON? Commented Nov 5, 2013 at 13:49

2 Answers 2

1

All you want to do is add a few properties to each object stored in your array. That's pretty trivial: iterate through the array, adding the required properties to the current element.

function addPrefix(rawJSON) {
    var yourArray = JSON.parse(rawJSON); // parse the JSON string to an actual array
    $.each(yourArray, function(index, element) {
        element.data = element.id;
        element.metadata = {id : element.name};
    });
    return JSON.stringify(yourArray); // converts the array, with the new properties, back to a JSON string
}

NOTE: The code above assumes you're passing a string as the argument to the addPrefix function, because that's what JSON is. It's a data interchange format for passing information between languages, and is transported as a string. What you've posted in the question is actually a JavaScript array. There's no such thing as a "JSON array"; you either have an array or you have a JSON string that represents one. Saying "JSON array" should mean the latter (though you shouldn't use it at all), but people frequently use it to mean the former.

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

5 Comments

ALMOST THERE!!! You Da Man Tony!!! - It's adding the elements to the END of the array... I simply need to modify the script to add to the BEGINNING of each array... Should we just build a new array...??? On the fly? Here's what I need in the simplist form: [ { "data": "Network 1", "metadata": {"id" : "n1"}, <-- This is where the ORIGINAL array will live ---> }]
It's not an array; it's a dictionary/object, so the order doesn't matter.
Actually, for the use I'm having, JSTree, the order DOES matter... any thoughts?
@Peter It's an object, there's zero guarantee in JavaScript of the iteration order.
Ah, you're 100% right... another Homer Simpson moment... now, it's coming through nicely... and the ONLY problem I have and it's got to be minor, is that when I copy and paste the JSON Object into a txt file and call that, the tree builds successfully. That being said, if I call the function that RETURNS the tree, like you coded, the treeview hangs and doesn't render. I've sent the JSON object to the console.log and view it through it's process and sure enough, there's not a single difference... my theory is that the prefix of a double quote before the [ and after the ] may be suspect...
0

Easiest way to modify array structures is to use - map. I prefer underscore as some browser still do not have support for this function.

You can easily do this:

data = _(data).map(function(value, index) { 
  value.push(some_more_data);
  return value; 
});

1 Comment

I don't think push returns a new array.

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.