Home > Back-end >  How to create a json object from another json object using JavaScript?
How to create a json object from another json object using JavaScript?

Time:02-24

I am really junior with javascript and json, so I have this JSON input, and I need to get all that information in the "properties" object to create a new JSON object with just that information.

I'm using a base code like this one, but this is just returning {}.

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

Original 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": "​"
        }
    }

I'm sure that it will be a simple solution, but need to solve this asap.

Thanks

CodePudding user response:

You appear to be trying to grab properties from the wrong object. It should be value not 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","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":"​"}}}';

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);

Additional information

CodePudding user response:

use this 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"}`))

CodePudding user response:

if you want a completely new object

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

or

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