使用节点JS合并两个不同的JSON

时间:2017-11-16 05:44:16

标签: json

我有两个请求使用NodeJS Seriate将数据作为JSON返回。

第一个回应是:

{
    "status": true,
    "message": "Data Found",
    "data": [
        {
            "statusCode": 200,
            "body": {
                "expand": "renderedFields,names,schem,operations,editmeta,changelog,versionedRepresentations",
                "id": "64672",
                "self": "https://computenext.atlassian.net/rest/api/2/issue/64672",
                "key": "CKS-2016",
                "fields": {
                    "parent": {
                        "id": "64670",
                        "key": "CKS-2014"
                    }
                }
            }
        }
    ]
}

第二个回应是:

{
    "statusCode": 200,
    "body": {
        "errors": [],
        "detail": [
            {
                "repositories": [
                    {
                        "name": "registry",
                        "avatar": "http://stash.computenext.com/projects/SERVICES/avatar.png?s=32",
                        "avatarDescription": "services"
                    }
                ]
            }
        ]
    }
}

我想合并两个响应,它们具有以下结构:

{
    "status": true,
    "message": "Data Found",
    "data": [
        {
            "statusCode": 200,
            "body": {
                "expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
                "id": "64672",
                "self": "https://computenext.atlassian.net/rest/api/2/issue/64672",
                "key": "CKS-2016",
                "fields": {
                    "parent": {
                        "id": "64670",
                        "key": "CKS-2014",
                        "detail": [
                            {
                                "repositories": [
                                    {
                                        "name": "registry",
                                        "avatar": "http://stash.computenext.com/projects/SERVICES/avatar.png?s=32",
                                        "avatarDescription": "services"
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
        }
    ]
}

我搜索并找到了几种连接或扩展两个JSON对象的方法,但没有类似的方法。

我怎样才能做到这一点?

我的实际代码是,

exports.getIssues = function(req, res) {
    console.log(filename + '>>get Issues>>');
    var response = {
        status : Boolean,
        message : String,
        data : String
    };
    var request = require('request');
    var username = const.username ;
    var password = const.password ;
    var options = {
        url : 'https://computenext.atlassian.net/rest/api/2/search?jql=status+%3D+Resolved+ORDER+BY+updated',
        auth : {
            username : username,
            password : password
        }
    };

    request( options, function(error, obj) {
                if (error) {
                    response.message = appmsg.DATA_NT_FOUND;
                    response.status = false;
                    response.data = obj;
                    res.send(response);
                } else {
                     response.message = appmsg.DATA_FOUND;
                     response.status = true;
                     response.data = JSON.parse(obj.body);
                     //res.send(response);
                    var issueKey = response.data.issues;

                    // var keyData = issueKey[0].key;
                    // console.log(response.data.issues);
                    // console.log(keyData);
                    var output = [];
                    for(var i = 0; i < issueKey.length; i++) {
                        var issue = issueKey[i].key;
                        //var key = [];
                        //key.push(issue);
                        console.log(issue);

                        var respon = {
                            status : Boolean,
                            message : String,
                            data : String
                        };
                            var request = require('request'),
                                username = const.username ,
                                password = const.username ,
                                url = "https://computenext.atlassian.net/rest/api/2/issue/" + issue,
                                auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

                            //console.log(url);
                            request({url : url,headers : {"Authorization" : auth}}, function(err, object){
                                if (object) {

                                        var info = object;
                                        output.push(info); // this is not working as ouput is undefined at this    point
                                        //var pout = JSON.parse(output);
                                        //console.log(info);
                                        console.log("==============================================================================");
                                        //console.log(output);
                                        console.log("******************************************************************************");
                                            if(issueKey.length === output.length){
                                                respon.message = appmsg.DATA_FOUND;
                                                respon.status = true;
                                                respon.data = output;
                                                //console.log(output);
                                                //res.send(respon);


                                                  var id = issueKey[0].id;
                                                  console.log(id);

                                                   var commitout = [];
                                                    for(var i = 0; i < issueKey.length; i++) {
                                                        var commits = issueKey[i].id;
                                                        console.log(commits);



                                                      var request = require('request'),
                                                      username = const.username ,
                                                      password = const.password ,
                                                      url = "https://computenext.atlassian.net/rest/dev-status/1.0/issue/detail?issueId=" + commits + "&applicationType=stash&dataType=repository",
                                                      auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

                                                    //console.log(url);
                                                    var test = [];
                                                    request({url : url,headers : {"Authorization" : auth}}, function(err, obj1){
                                                      if (obj1) {

                                                              var info1 = obj1.body;
                                                              commitout.push(info1);
                                                              if(issueKey.length === commitout.length){
                                                                respon.message = appmsg.DATA_FOUND;
                                                                respon.status = true;
                                                                respon.data = commitout;
                                                                // console.log(commitout);
                                                                  //var test = merge(output, commitout);

                                                                  var text = output.body;
                                                                  var resultdone = output;

                                                                  resultdone.data = resultdone + commitout.body;

                                                                  console.log(resultdone.data);


                                                                res.send(resultdone.data);
                                                              }
                                                            }
                                                   });
                                                  }

                                    }
                                }

                        });

                }
            }

        });

};

  

如何合并这两个数组?在一个回应中。这是我的问题..

3 个答案:

答案 0 :(得分:1)

永远不要直接编写JSON字符串。先解析它。

const firstRes = JSON.parse(firstResJson);
const secondRes = JSON.prase(secondResJson);

现在,有点不清楚你想做什么,为什么你想这样做,但试试这个:

firstRes.data.body.fields.parent.detail = secondRes.body.detail;

然后,您会在firstRes中找到合并后的数据。要将它恢复为JSON:

JSON.stringify(firstRes);

答案 1 :(得分:0)

var obj1  = {
"status": true,
"message": "Data Found",
"data": [
    {
        "statusCode": 200,
        "body": {
            "expand": "renderedFields,names,schem,operations,editmeta,changelog,versionedRepresentations",
            "id": "64672",
            "self": "https://computenext.atlassian.net/rest/api/2/issue/64672",
            "key": "CKS-2016",
            "fields": {
                "parent": {
                    "id": "64670",
                    "key": "CKS-2014"
                }
            }
        }
    }
]};


var obj2 = {
"statusCode": 200,
"body": {
    "errors": [],
    "detail": [
        {
            "repositories": [
                {
                    "name": "registry",
                    "avatar": "http://stash.computenext.com/projects/SERVICES/avatar.png?s=32",
                    "avatarDescription": "services"
                }
            ]
        }
    ]}
   }


  obj1.data.forEach(function(item, index){
      item.body.fields.parent.detail = r.body.detail[index];
  });

  console.log(obj1);

答案 2 :(得分:-1)

var result1 =

{
    "status": true,
    "message": "Data Found",
    "data": [
        {
            "statusCode": 200,
            "body": {
                "expand": "renderedFields,names,schem,operations,editmeta,changelog,versionedRepresentations",
                "id": "64672",
                "self": "https://computenext.atlassian.net/rest/api/2/issue/64672",
                "key": "CKS-2016",
                "fields": {
                    "parent": {
                        "id": "64670",
                        "key": "CKS-2014"
                    }
                }
            }
        }
    ]
}    

var text = result1.data [0] .body;

var result2 =

{
    "statusCode": 200,
    "body": {
        "errors": [],
        "detail": [
            {
                "repositories": [
                    {
                        "name": "registry",
                        "avatar": "http://stash.computenext.com/projects/SERVICES/avatar.png?s=32",
                        "avatarDescription": "services"
                    }
                ]
            }
        ]
    }
}; 

var resultdone = result1;

resultdone.data [0] .body.fields.parent = result2.body.detail;