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
}
7
  • 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. Commented Nov 21, 2018 at 5:30
  • @certainPerformance sorry for the inconvenience, tagged java by mistake Commented Nov 21, 2018 at 5:33
  • so what's your question? Commented Nov 21, 2018 at 5:45
  • @sean how to add the first object into the second object? Commented Nov 21, 2018 at 5:45
  • I thought you have answer the question, loop thought the parameter fields, if empty, replace it with the first object Commented Nov 21, 2018 at 5:56

1 Answer 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?

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

1 Comment

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.

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.