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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.