add object to a complex json in javascript












0















Suppose there are two json objects as



1.



{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
}


or



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": null
}


and



2.



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
}


i.e
the second object will have ("conditionTemp", "value", "variable"),



the first "conditionTemp" will have "functionID", "parameters"



inside "parameters" we can have any no. of objects. If inside parameters, the the object's "conditionTemp" value is not null, we have to check the parameter object inside of that. If the parameter object is empty, we have to insert the **first object there.**



So for the above jsons, on adding the first object onto the second,
the resultant json will be



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
}









share|improve this question




















  • 4





    Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.

    – CertainPerformance
    Nov 21 '18 at 5:30











  • @certainPerformance sorry for the inconvenience, tagged java by mistake

    – args
    Nov 21 '18 at 5:33











  • so what's your question?

    – Sean
    Nov 21 '18 at 5:45











  • @sean how to add the first object into the second object?

    – args
    Nov 21 '18 at 5:45











  • I thought you have answer the question, loop thought the parameter fields, if empty, replace it with the first object

    – Sean
    Nov 21 '18 at 5:56
















0















Suppose there are two json objects as



1.



{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
}


or



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": null
}


and



2.



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
}


i.e
the second object will have ("conditionTemp", "value", "variable"),



the first "conditionTemp" will have "functionID", "parameters"



inside "parameters" we can have any no. of objects. If inside parameters, the the object's "conditionTemp" value is not null, we have to check the parameter object inside of that. If the parameter object is empty, we have to insert the **first object there.**



So for the above jsons, on adding the first object onto the second,
the resultant json will be



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
}









share|improve this question




















  • 4





    Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.

    – CertainPerformance
    Nov 21 '18 at 5:30











  • @certainPerformance sorry for the inconvenience, tagged java by mistake

    – args
    Nov 21 '18 at 5:33











  • so what's your question?

    – Sean
    Nov 21 '18 at 5:45











  • @sean how to add the first object into the second object?

    – args
    Nov 21 '18 at 5:45











  • I thought you have answer the question, loop thought the parameter fields, if empty, replace it with the first object

    – Sean
    Nov 21 '18 at 5:56














0












0








0








Suppose there are two json objects as



1.



{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
}


or



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": null
}


and



2.



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
}


i.e
the second object will have ("conditionTemp", "value", "variable"),



the first "conditionTemp" will have "functionID", "parameters"



inside "parameters" we can have any no. of objects. If inside parameters, the the object's "conditionTemp" value is not null, we have to check the parameter object inside of that. If the parameter object is empty, we have to insert the **first object there.**



So for the above jsons, on adding the first object onto the second,
the resultant json will be



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
}









share|improve this question
















Suppose there are two json objects as



1.



{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
}


or



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": null
}


and



2.



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
}


i.e
the second object will have ("conditionTemp", "value", "variable"),



the first "conditionTemp" will have "functionID", "parameters"



inside "parameters" we can have any no. of objects. If inside parameters, the the object's "conditionTemp" value is not null, we have to check the parameter object inside of that. If the parameter object is empty, we have to insert the **first object there.**



So for the above jsons, on adding the first object onto the second,
the resultant json will be



{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
}






javascript arrays json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 6:33







args

















asked Nov 21 '18 at 5:29









argsargs

84




84








  • 4





    Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.

    – CertainPerformance
    Nov 21 '18 at 5:30











  • @certainPerformance sorry for the inconvenience, tagged java by mistake

    – args
    Nov 21 '18 at 5:33











  • so what's your question?

    – Sean
    Nov 21 '18 at 5:45











  • @sean how to add the first object into the second object?

    – args
    Nov 21 '18 at 5:45











  • I thought you have answer the question, loop thought the parameter fields, if empty, replace it with the first object

    – Sean
    Nov 21 '18 at 5:56














  • 4





    Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.

    – CertainPerformance
    Nov 21 '18 at 5:30











  • @certainPerformance sorry for the inconvenience, tagged java by mistake

    – args
    Nov 21 '18 at 5:33











  • so what's your question?

    – Sean
    Nov 21 '18 at 5:45











  • @sean how to add the first object into the second object?

    – args
    Nov 21 '18 at 5:45











  • I thought you have answer the question, loop thought the parameter fields, if empty, replace it with the first object

    – Sean
    Nov 21 '18 at 5:56








4




4





Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.

– CertainPerformance
Nov 21 '18 at 5:30





Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately.

– CertainPerformance
Nov 21 '18 at 5:30













@certainPerformance sorry for the inconvenience, tagged java by mistake

– args
Nov 21 '18 at 5:33





@certainPerformance sorry for the inconvenience, tagged java by mistake

– args
Nov 21 '18 at 5:33













so what's your question?

– Sean
Nov 21 '18 at 5:45





so what's your question?

– Sean
Nov 21 '18 at 5:45













@sean how to add the first object into the second object?

– args
Nov 21 '18 at 5:45





@sean how to add the first object into the second object?

– args
Nov 21 '18 at 5:45













I thought you have answer the question, loop thought the parameter fields, if empty, replace it with the first object

– Sean
Nov 21 '18 at 5:56





I thought you have answer the question, loop thought the parameter fields, if empty, replace it with the first object

– Sean
Nov 21 '18 at 5:56












1 Answer
1






active

oldest

votes


















1














First level would be like this:






var obj_a = {
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
};

var obj_b = {
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
};

var final_obj = Object.keys(obj_a).reduce(function(data, key) {
if (obj_a[key] == null && obj_b[key] != null)
data[key] = obj_b[key];
else
data[key] = obj_a[key];

return data;
}, {});

console.log(final_obj);





Second and further levels would be tricky. Is the format always like that? In your example, values could be (string, null, array of objects)... are there other formats not mentioned or that you wouldn't know?






share|improve this answer
























  • Yes, the format will always be the same, and also if conditionTemp is null, then "value" and "variableValue" will have some value, can be string or integers, else they will be null. conditionTemp can be null or object with functionID and parameters.

    – args
    Nov 21 '18 at 6:40













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405765%2fadd-object-to-a-complex-json-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














First level would be like this:






var obj_a = {
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
};

var obj_b = {
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
};

var final_obj = Object.keys(obj_a).reduce(function(data, key) {
if (obj_a[key] == null && obj_b[key] != null)
data[key] = obj_b[key];
else
data[key] = obj_a[key];

return data;
}, {});

console.log(final_obj);





Second and further levels would be tricky. Is the format always like that? In your example, values could be (string, null, array of objects)... are there other formats not mentioned or that you wouldn't know?






share|improve this answer
























  • Yes, the format will always be the same, and also if conditionTemp is null, then "value" and "variableValue" will have some value, can be string or integers, else they will be null. conditionTemp can be null or object with functionID and parameters.

    – args
    Nov 21 '18 at 6:40


















1














First level would be like this:






var obj_a = {
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
};

var obj_b = {
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
};

var final_obj = Object.keys(obj_a).reduce(function(data, key) {
if (obj_a[key] == null && obj_b[key] != null)
data[key] = obj_b[key];
else
data[key] = obj_a[key];

return data;
}, {});

console.log(final_obj);





Second and further levels would be tricky. Is the format always like that? In your example, values could be (string, null, array of objects)... are there other formats not mentioned or that you wouldn't know?






share|improve this answer
























  • Yes, the format will always be the same, and also if conditionTemp is null, then "value" and "variableValue" will have some value, can be string or integers, else they will be null. conditionTemp can be null or object with functionID and parameters.

    – args
    Nov 21 '18 at 6:40
















1












1








1







First level would be like this:






var obj_a = {
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
};

var obj_b = {
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
};

var final_obj = Object.keys(obj_a).reduce(function(data, key) {
if (obj_a[key] == null && obj_b[key] != null)
data[key] = obj_b[key];
else
data[key] = obj_a[key];

return data;
}, {});

console.log(final_obj);





Second and further levels would be tricky. Is the format always like that? In your example, values could be (string, null, array of objects)... are there other formats not mentioned or that you wouldn't know?






share|improve this answer













First level would be like this:






var obj_a = {
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
};

var obj_b = {
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
};

var final_obj = Object.keys(obj_a).reduce(function(data, key) {
if (obj_a[key] == null && obj_b[key] != null)
data[key] = obj_b[key];
else
data[key] = obj_a[key];

return data;
}, {});

console.log(final_obj);





Second and further levels would be tricky. Is the format always like that? In your example, values could be (string, null, array of objects)... are there other formats not mentioned or that you wouldn't know?






var obj_a = {
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
};

var obj_b = {
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
};

var final_obj = Object.keys(obj_a).reduce(function(data, key) {
if (obj_a[key] == null && obj_b[key] != null)
data[key] = obj_b[key];
else
data[key] = obj_a[key];

return data;
}, {});

console.log(final_obj);





var obj_a = {
"conditionTemp": {
"functionID": "func_1",
"parameters": [{
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
},
{
"conditionTemp": {
"functionID": "func_1",
"parameters": [{}]
},
"value": null,
"variableValue": "null"
},
{}
]
},
"value": null,
"variableValue": null
};

var obj_b = {
"conditionTemp": null,
"value": null,
"variableValue": "flowParameters_3"
};

var final_obj = Object.keys(obj_a).reduce(function(data, key) {
if (obj_a[key] == null && obj_b[key] != null)
data[key] = obj_b[key];
else
data[key] = obj_a[key];

return data;
}, {});

console.log(final_obj);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 6:32









ACDACD

8941112




8941112













  • Yes, the format will always be the same, and also if conditionTemp is null, then "value" and "variableValue" will have some value, can be string or integers, else they will be null. conditionTemp can be null or object with functionID and parameters.

    – args
    Nov 21 '18 at 6:40





















  • Yes, the format will always be the same, and also if conditionTemp is null, then "value" and "variableValue" will have some value, can be string or integers, else they will be null. conditionTemp can be null or object with functionID and parameters.

    – args
    Nov 21 '18 at 6:40



















Yes, the format will always be the same, and also if conditionTemp is null, then "value" and "variableValue" will have some value, can be string or integers, else they will be null. conditionTemp can be null or object with functionID and parameters.

– args
Nov 21 '18 at 6:40







Yes, the format will always be the same, and also if conditionTemp is null, then "value" and "variableValue" will have some value, can be string or integers, else they will be null. conditionTemp can be null or object with functionID and parameters.

– args
Nov 21 '18 at 6:40






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405765%2fadd-object-to-a-complex-json-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini