簡體   English   中英

如何使用 JavaScript 從另一個 json object 創建一個 json object?

[英]How to create a json object from another json object using JavaScript?

我真的很初級 JavaScript 和 json,所以我有這個 JSON 輸入,我需要在“屬性”object 中獲取所有信息,以創建一個新的 JSON object 僅包含該信息。

我正在使用類似這樣的基本代碼,但這只是返回 {}。

exports.step = function(input, fileInput) {
    var alert = {
        'Properties': input.alert.properties
    }
    return JSON.stringify(alert, undefined, 1);
  };

原JSON:

"value": {
        "id": "12345",
        "entity": {
            "_integrationDefinitionId": "7a6764",
            "_integrationName": "Apple Main",
            "_beginOn": "2021-09-01T02:20:06.189Z",
            "displayName": "apple-onev",
            "_accountIdPartitioned": "12345|12",
            "_class": [
                "Deployment",
                "Group"
            ],
            "_version": 3,
            "_integrationClass": [
                "CiSSP",
                "Infrastructure"
            ],
            "_accountId": "123456",
            "_id": "1e234567",
            "_key": "arn:aws:autoscaling:us-west-2:83712398:autoScalingGroup:asd1238-20c8-41aa-bcec-12340912341:autoScalingGroupName/awseb-e-juancito-stack-AWSEBAutoScalingGroup-123456",
            "_type": [
                "aws_autoscaling_group"
            ],
            "_deleted": false,
            "_rawDataHashes": "1233456==",
            "_integrationInstanceId": "54321",
            "_integrationType": "aws",
            "_source": "integration",
            "_createdOn": "2021-07-19T23:19:19.758Z"
        },
        "properties": {
            "webLink": "https://google.com",
            "arn": "name",
            "region": "us-west-2",
            "name": "JonnyAndTheVibes",
            "launchConfigurationName": "OtherName",
            "minSize": 1,
            "maxSize": 4,
            "desiredCapacity": 1,
            "defaultCooldown": 360,
            "availabilityZones": "us-west-2a",
            "LoadBalancerNames": "MoreInfo",
            "healthCheckType": "EC2",
            "healthCheckGracePeriod": 0,
            "instanceIds": "InstanceName",
            "subnetIds": "subnet",
            "terminationPolicies": "Default",
            "newInstancesProtectedFromScaleIn": false,
            "serviceLinkedRoleARN": "aMoreInfo",
            "tag.Name": "atag",
            "tag.application": "othertag",
            "tag.aws:cloudformation:logical-id": "moretagsp",
            "tag.aws:cloudformation:stack-id": "taggigante",
            "tag.aws:cloudformation:stack-name": "ydaleconlostags",
            "tag.elasticbeanstalk:environment-id": "seguimosmetiendoletags",
            "tag.elasticbeanstalk:environment-name": "tag",
            "tag.env": "tag",
            "tag.team": "tag",
            "accountId": "tag",
            "tag.AccountName": "tag",
            "tag.Production": true,
            "@tag.Production": "​"
        }
    }

我相信這將是一個簡單的解決方案。

您似乎試圖從錯誤的 object 獲取properties 。它應該是value而不是alert

 const json = '{"value":{"id":"12345","entity":{"_integrationDefinitionId":"7a6764","_integrationName":"Apple Main","_beginOn":"2021-09-01T02:20:06.189Z","displayName":"apple-onev","_accountIdPartitioned":"12345|12","_class":["Deployment","Group"],"_version":3,"_integrationClass":["CiSSP","Infrastructure"],"_accountId":"123456","_id":"1e234567","_key":"arn:aws:autoscaling:us-west-2:83712398:autoScalingGroup:asd1238-20c8-41aa-bcec-12340912341:autoScalingGroupName/awseb-e-juancito-stack-AWSEBAutoScalingGroup-123456","_type":["aws_autoscaling_group"],"_deleted":false,"_rawDataHashes":"1233456==","_integrationInstanceId":"54321","_integrationType":"aws","_source":"integration","_createdOn":"2021-07-19T23:19:19.758Z"},"properties":{"webLink":"https://google.com","arn":"name","region":"us-west-2","name":"JonnyAndTheVibes","launchConfigurationName":"OtherName","minSize":1,"maxSize":4,"desiredCapacity":1,"defaultCooldown":360,"availabilityZones":"us-west-2a","LoadBalancerNames":"MoreInfo","healthCheckType":"EC2","healthCheckGracePeriod":0,"instanceIds":"InstanceName","su.netIds":"su.net","terminationPolicies":"Default","newInstancesProtectedFromScaleIn":false,"serviceLinkedRoleARN":"aMoreInfo","tag.Name":"atag","tag.application":"othertag","tag.aws:cloudformation:logical-id":"moretagsp","tag.aws:cloudformation:stack-id":"taggigante","tag.aws:cloudformation:stack-name":"ydaleconlostags","tag.elasticbeanstalk:environment-id":"seguimosmetiendoletags","tag.elasticbeanstalk:environment-name":"tag","tag.env":"tag","tag.team":"tag","accountId":"tag","tag.AccountName":"tag","tag.Production":true,"@tag.Production":""}}}'; function getAlert(dsta) { // Destructure the properties object from the // data's value property const { properties } = data.value; // Create a new object with it const alert = { properties }; // Return the string return JSON.stringify(alert, null, 2); }; // Parse the JSON const data = JSON.parse(json); // Call the function with the parsed data const alert = getAlert(data); console.log(alert);

附加信息

如果你想要一個全新的 object

var newJsonObject = JSON.parse('{ "properties":'
 + JSON.stringify (origJson.value.properties) + "}");

或者

var newJsonObject={"properties":Object.assign ({}, origJson.value.properties)};

使用這個 function:

 function assignJsons(...jsons) { const convertToObject = jsons.map(json => { return JSON.parse(json) }); return JSON.stringify(Object.assign(...convertToObject)) } //test console.log(assignJsons(`{"name": "alex", "family": "mask"}`, `{"family": "rejest"}`))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM