Merge JSON objects through unique field then print child node data

Related searches

I have the following JSON:

data: {
    questions: "[{"id":"vzDDWL3GQvJi","title":"This is question 1","type":"opinion_scale","ref":"data_q1","properties":[]},{"id":"okT0ieWJm74d","title":"This  is question 2","type":"opinion_scale","ref":"data_q2","properties":[]},

    answers: "[{"type":"number","number":2,"field":{"id":"vzDDWL3GQvJi","type":"opinion_scale","ref":"data_q1"}},{"type":"number","number":4,"field":{"id":"okT0ieWJm74d","type":"opinion_scale","ref":"data_q2"}},

    createdDate: "2020-02-14T07:43:02.000000Z"
}

A tidy version of the above is:

Question (questions object)

  • title : "This is question 1"
  • ref" : "data_q1"

Answer (answers object)

  • ref" : "data_q1"
  • number : 2

So, for question 1 (with the ref: data_q1) the number (score) is 2.

What I'm trying to do, is to merge both questions answers together based on ref. I want to do this so that I can get the number. I.e. question and answer for data_q1 is 2.

I have the following:

// Get questions
var questionData = data.data.questions;
var questions = JSON.parse(questionData);

// get answers
var answerData = data.data.answers;
var answers = JSON.parse(answerData);

What I've tried:

var answersInfo = answers.map( function(order) {
if( answers.ref === "RefIDHere"){
     var info = { "number": answers.number}
     return info;
 }
});
console.log(answersInfo);

However, the issue with the above is, in answers.ref ===, I don't know what to pass because the questions and answers haven't been mapped together yet.


Please try below solution.

finalResult = [];
questions.map( que => 
  answers.map(ans =>  {
    if(que.ref === ans.field.ref){
      finalResult.push({question: que, answer: ans})
} 
return finalResult; 
}));

Now if you see finalResult has the question and respective answer and you can access the number. Hope it helps.

Merge two json objects java, Java 14+: Java Record JSON Serialization and Deserialization with JSON-B Java Merge two complex objects in PHP I have two data objects converted from two objects, one from source and one from patch, a member with a field that is in “value” using JavaScript (4) same objects node Jul 08, 2015 � Merge 2 JSON to� Pretty Print JSON Object: In debugging, we alway like to print data to console to verify if its OK. If you are trying to see if a large JSON has something you are expecting, then its very hard to locate if its printed in flat structure. In Such cases, what you need is pretty printing the JSON object.


1) Build an answer_number object which has ref as key and value as number from data.answers. 2) Use map over data.questions and add the number value from above. Hope this helps.

const data = {
  questions: [
    {
      id: "vzDDWL3GQvJi",
      title: "This is question 1",
      type: "opinion_scale",
      ref: "data_q1",
      properties: []
    },
    {
      id: "okT0ieWJm74d",
      title: "This  is question 2",
      type: "opinion_scale",
      ref: "data_q2",
      properties: []
    }
  ],
  answers: [
    {
      type: "number",
      number: 2,
      field: { id: "vzDDWL3GQvJi", type: "opinion_scale", ref: "data_q1" }
    },
    {
      type: "number",
      number: 4,
      field: { id: "okT0ieWJm74d", type: "opinion_scale", ref: "data_q2" }
    }
  ],
  createdDate: "2020-02-14T07:43:02.000000Z"
};

const answers_number = data.answers.reduce(
  (acc, curr) => Object.assign(acc, { [curr.field.ref]: curr.number }),
  {}
);

const questions_count = data.questions.map(que => ({
  ...que,
  number: answers_number[que.ref]
}));

console.log(questions_count);

How to Merge/Concat Multiple JSONObjects in Java? Best way to , JSONObject is an unordered collection of name/value pairs and widely used in. Merge Two JSON Objects Utility by Crunchify in Java Enterprise applications for data transfer between client / server over REST interface. a LinkedList Class From Scratch & Enable Pretty Print in JSON Processing API� The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Python script.


Use filter:

let data = {
    questions: '[{"id":"vzDDWL3GQvJi","title":"This is question 1","type":"opinion_scale","ref":"data_q1","properties":[]},{"id":"okT0ieWJm74d","title":"This  is question 2","type":"opinion_scale","ref":"data_q2","properties":[]}]',
    answers: '[{"type":"number","number":2,"field":{"id":"vzDDWL3GQvJi","type":"opinion_scale","ref":"data_q1"}},{"type":"number","number":4,"field":{"id":"okT0ieWJm74d","type":"opinion_scale","ref":"data_q2"}}]',
    createdDate: "2020-02-14T07:43:02.000000Z"
}

let getQuestionAndAnswer = (data, ref) => {
  let question = JSON.parse(data.questions).filter(v => v.ref === ref);
  let answer = JSON.parse(data.answers).filter(v => v.field.ref === ref);
  
  return {
      question,
      answer
    };
};

console.log(getQuestionAndAnswer(data, "data_q1"));

How to merge properties of two JavaScript objects dynamically , It is mostly used in the variable array where there is more than 1 values are We can merge different objects into one using the spread (…)� JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma.


Union of objects in javaScript based on unique value, Merging of two or more objects using object destructuring has same key, it simply merge the last objects value and have only one key value. JSON Data Set Sample. The JSON output from different Server APIs can range from simple to highly nested and complex. The examples on this page attempt to illustrate how the JSON Data Set treats specific formats, and gives examples of the different constructor options that allow the user to tweak its behavior.


javascript merge objects Code Example, Get code examples like "javascript merge objects" instantly right from your google search results with the Later properties overwrite earlier properties with the same name. */. 8 javascript by JavascriptNinja on Apr 29 2020 Donate //Fast cloning with data loss. 8. var studentCopy3 = JSON.parse(JSON.stringify(student ));. It is commonly used by web APIs to return data. If a message property contains a JSON string it must first be parsed to its equivalent JavaScript object before the properties it contains can be accessed. To determine whether a property contains a String or Object, the Debug node can be used. Node-RED provides a JSON node to do this conversion.


print. The print parameter lets us specify the format of our response from the database. Adding print=pretty to our request will return the data in a human-readable format. print=pretty is supported by GET, PUT, POST, PATCH, and DELETE requests. To suppress the output from the server when writing data, we can add print=silent to our request.