Add Key to JSON help

I have a JSON response coming in that looks like this:

[
  {
    "id": 0,
    "name": "string"
  }
]

I need to add a key value pair at the beginning, so my final JSON looks like this:


"NumberOfRecords": "3",

[
  {
    "id": 0,
    "name": "string"
  }
]


where number of records would equal the array length.

Can anyone help me get this working, please?

Thank you
John S.Web DeveloperAsked:
Who is Participating?
 
Vijaya KumarCommented:
try this

            var JSON = [{ "id": 0, "name": "string" },{ "id": 0, "name": "string" }];
            //console.log(JSON);
            
            JSON["Numberofrecords"] = "3";
            
            console.log(JSON);
            
            //if u have number of arrays then
            
            JSON[1].Numberofrecords = "4";
            
            console.log(JSON);

hopes it helps...
0
 
ste5anSenior DeveloperCommented:
hmm, why fiddling with an incoming response?

Something like this?

<!DOCTYPE html>
<html>
    <body>
        <script>
            function getJson() {
                return '[{ "id": 0, "name": "string" }]';
            }

            function expandJson(json) {
                var src = JSON.parse(json);
                var dst = new Object();
                dst.array = src;
                dst.NumberOfRecords = 3;
                return JSON.stringify(dst, null, 1);
            }

            console.log(expandJson(getJson()));
        </script>
    </body>
</html>

Open in new window


btw, properties must have names, cause they are internally stored as key-value pairs.
0
 
John S.Web DeveloperAuthor Commented:
I am getting the response from an API, so on line 6 above, I would pass the DATA from the AJAX call, is that correct?

Does this change your solution? ( Not at work yet, so haven't tried )
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ste5anSenior DeveloperCommented:
getJson() is just the function where the data comes from, thus a placeholder for your AJAX success callback or the then-promise.

But it sounds a little bit uncommon to do that. What is your concrete use-case?
0
 
Julian HansenCommented:
Why do you need to add a value to the returned JSON? If you need to know how many records there are just use the .length property.
0
 
John S.Web DeveloperAuthor Commented:
The reason I am doing this, is so that I can take my newly created object and then pass that onto a plugin. There are more custom key value pairs that I need to add, but for the sake of brevity I only added the NumberOfRecords.
0
 
John S.Web DeveloperAuthor Commented:
Thanks to all. Both worked perfect, and more importantly, I learned a little more about objects today! :)
0
 
ste5anSenior DeveloperCommented:
For learning purposes: Just try to spot the relevant difference between my solution and Vijaya's..
2
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.