0

when my ajax call completes an array of json is returnedenter image description here

for my angular data binding to work perfectly, i need to merge all values in to a single JSON file. I have tried $.extend(), it's giving following output enter image description here

Need a solution for this

for example if my response looks like this:

[0:"{'test':'test'}", 1:"{'test':'test'}", 2:"{'test':'test'}",3: "{'test':'test'}"];

the output i need is :

{ test':'test', 'test':'test', 'test':'test', 'test':'test' }

Edit: The final value will be associated to the ng-model automatically.

desired output example:

{
"unique_id": 172,
"portfolio": "DIGITAL",
"bus_unit": "dummy",
"project_phase": "",
"test_phase": "SIT",
"project": "Google",
"golivedate": "03/09/2016",
"performance": "Green",
"summary": "jgnbfklgnflknflk",
"last_updated": "",
"risks_issues": "gfmngfnfglkj",
"project_start": "03/16/2016",
"batchLast_run": "",
"custom_project": "1",
"test_execution_id": 5456,
"unique_id": 172,
"test_execution_id": 5456,
"pass": 8,
"fail": 8,
"blocked": 8,
"in_progress": 8,
"no_run": 8,
"not_available": 0,
"total": 8  
}
4
  • can you post your response properly ? Commented Mar 28, 2016 at 12:13
  • And also describe what exactly i need to merge all values in to a single JSON file mean? Commented Mar 28, 2016 at 12:16
  • Should the final result be an object or a string? Commented Mar 28, 2016 at 12:19
  • 1
    You are getting downvoted because your question is not clear. Commented Mar 28, 2016 at 12:21

4 Answers 4

2

From what I understand you are trying to convert array of Json data into one singel json data. So you have array of values but you would want all of them in one variable. Try this

var testData = ["{'test':'test'}", "{'test':'test'}", "{'test':'test'}", "{'test':'test'}"];

var finalData ="";

$.each(testData,function(index,value){
   finalData += value +',';
});

finalData = finalData.replace(/\},\{/g,',').slice(0, -1);

document.write(finalData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

5 Comments

nope your answer outputs {'test':'test'},{'test':'test'},{'test':'test'},{'test':'test'} what i need is {test':'test','test':'test','test':'test','test':'test'}
hi my response looks like [0:"{'test':'test'}", 1:"{'test':'test'}", 2:"{'test':'test'}",3: "{'test':'test'}"];
@VigneshRajarajan updated my answer. let me know if it helps
you're my Savior, my messiah :D
@VigneshRajarajan happy coding!! Mapule :D
1

Map just applies the function to every element in the array.

var arrayOfJSON = ...
var arrayOfObjects = arrayOfJSON.map(function (jsonString){
    return JSON.parse(jsonString)
})
var jsonStringWithAllObjects = JSON.stringify(arrayOfObjects)

Comments

1

If you use underscore.js then can easily

like:

var list = [{"test1": "test1"}, {"test2": "test2"}, {"test3": "test3"}];
var newList = _.extend.apply(null,[{}].concat(list));

then output will be

{ test1: "test1", test2: "test2", test3: "test3" }

3 Comments

Can you explain? I don't understand how that code works?
Copy all of the properties in the source objects over to the destination object, and return the destination object.
@shaishabroy that would be difficult because all the source objects have the same key. So destination in the end will just have one property. right??
0

Iterate over the array, convert every value to a JSON object, concatenate and then convert to a string back.

If you need fo this more times, you probably should make this a function.

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.