I have the following script that outputs a json formatted object:
function test() {
autoscaling.describeAutoScalingGroups(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(JSON.stringify(data)); // successful response
});
}
test();
This outputs the following json:
{
"ResponseMetadata": {
"RequestId": "##################"
},
"AutoScalingGroups": [
{
"AutoScalingGroupName": "################",
"AutoScalingGroupARN": "arn:aws:autoscaling:eu-west-1:#########:autoScalingGroup:###########",
"LaunchConfigurationName": "######-LC-###########",
"MinSize": 0,
"MaxSize": 0,
"DesiredCapacity": 0,
"DefaultCooldown": 300,
"AvailabilityZones": [
"eu-west-1b",
"eu-west-1c",
"eu-west-1a"
],
"LoadBalancerNames": [
"#########-ELB-###########"
],
"TargetGroupARNs": [
],
"HealthCheckType": "ELB",
"HealthCheckGracePeriod": 300,
"Instances": [
],
"CreatedTime": "2017-11-08T18:22:05.093Z",
"SuspendedProcesses": [
{
"ProcessName": "Terminate",
"SuspensionReason": "User suspended at 2017-11-08T18:22:14Z"
}
],
"VPCZoneIdentifier": "subnet-######,subnet-#######,subnet-#######",
"EnabledMetrics": [
],
"Tags": [
{
"ResourceId": "#######-ASG-##########",
"ResourceType": "auto-scaling-group",
"Key": "aws:cloudformation:logical-id",
"Value": "ASG",
"PropagateAtLaunch": true
},
{
"ResourceId": "#######-ASG-#########",
"ResourceType": "auto-scaling-group",
"Key": "aws:cloudformation:stack-id",
"Value": "arn:aws:cloudformation:eu-west-1:########:stack/##############",
"PropagateAtLaunch": true
},
{
"ResourceId": "################",
"ResourceType": "auto-scaling-group",
"Key": "aws:cloudformation:stack-name",
"Value": "#######",
"PropagateAtLaunch": true
}
],
"TerminationPolicies": [
"Default"
],
"NewInstancesProtectedFromScaleIn": false
}
]
}
I need to get the value of "SuspendedProcesses":[{"ProcessName": (see above)
Then if the value of "ProcessName" == "Terminate" (as it is above) do this else do this.
I know how to construct the if else syntax but how do I establish the value of "ProcessName" from the JSON output beforehand?
I also know how to manipulate an array created in a script but I'm having difficulty here because the json object is being created by the test() function so the normal rules don't seem to apply.
Any help would be appreciated.
Thanks
First, replace your console.log() calls with return statements. Then you can just do
var json = test();
var processes = json["AutoScalingGroups"][0]["Suspended Processes"]
updated code:
function test() {
autoscaling.describeAutoScalingGroups(params, function(err, data) {
if (err) {
return [err, err.stack]; // an error occurred
} else {
var json = JSON.stringify(data); // successful response
return json["AutoScalingGroups"][0]["Suspended Processes"];
}
});
}
var processes = test()
console.log(processes);
thanks #RobbieMilejczak, problem resolved:
First, replace your console.log() calls with return statements. Then you can just do
var json = test();
var processes = json["AutoScalingGroups"][0]["Suspended Processes"]
updated code:
function test() {
autoscaling.describeAutoScalingGroups(params, function(err, data) {
if (err) {
return [err, err.stack]; // an error occurred
} else {
var json = JSON.stringify(data); // successful response
return json["AutoScalingGroups"][0]["Suspended Processes"];
}
});
}
var processes = test()
console.log(processes);
Related
I want to read the values of key, access, path, bucket and bucketPath and use them in the JSON file test.json.
I have a function that reads the content of configuration.js and attempts to write to test.json. Currently, I am able to write the values of bucket.I get the changed/new values and lines of null for the rest of the json.
I want to always return the new values and the other objects in the file. Also, in cases where bucket already has a value, I want it replaced by whatever is read from configuration.json
How can I fix this, and how can i change the values for the rest access, key, path and bucketpath?
index.js
const fs = require("fs").promises;
async function readJSON(filePath, values) {
const data = await fs.readFile(filePath);
try {
return JSON.parse(data);
} catch (err) {
console.log(err);
}
}
(async() => {
const credentials = await readJSON("./configuration.json");
const path = credentials.path;
const bucket = credentials.bucket;
const access = credentials.access;
const key = credentials.key;
const bucketPath = credentials.bucketPath;
const data = await jsonReader("./test.json");
const finalJSON = data.data ? .map((x) => {
if (x.type == "s3 credentials") return { ...x, bucket };
});
await fs.writeFile(
"./test.json",
JSON.stringify({
data: finalJSON
})
);
})();
test.json
{
"label": "storage record",
"data": [{
"id": "8902uw",
"type": "config",
"values": {
"access": "$access",
"key": "$key"
}
},
{
"id": "893wh002jei",
"type": "s3 credentials",
"bucket": ""
},
{
"id": "90yueps",
"type": "upload",
"input": "localhost: `$path`"
},
{
"id": "9028901",
"type": "change",
"name": "Adjust data",
"measure": [{
"t": "setter"
},
{
"p": "filename",
"to": "$join([\"$bucketPath\", data])"
}
],
"fixed": ""
}
]
}
configuration.json
{
"key": "880082",
"access": "793082",
"path": "/store",
"bucket": "testBucket",
"bucketPath": "/record"
}
Currently, when I run this, I get:
{
null,
"data": [{
null,
null,
null,
null
{
"id": "893wh002jei",
"type": "s3 credentials",
"bucket": ""
},
{
null,
null,
null
]
}
might this be a solution !
const fs = require('fs');
const fileName = './file.json';
const file = require(fileName);
file.key = "new value";
fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
if (err) return console.log(err);
console.log(JSON.stringify(file));
console.log('writing to ' + fileName);
});
[Updated answer]
From what you comment:
it's the same question. So when I run what I have, I get null for the other objects. I want test.json to remain the same just with updated values.
const testObj = await jsonReader("./test.json");
const finalJSON = {
...testObj,
data: testObj.data?.map((x) => {
if (x.type === 's3 credentials') {
return { ...x, bucket };
} else {
return x;
}
})
}
// which the code I gave above,
// the `finalJSON` now is the clone of the original from `test.json`
// plus updated values
await fs.writeFile(
"./test.json",
JSON.stringify(finalJSON)
);
[Original answer]
There is a problem with the function you pass to the map function.
The condition if without else.
I think you need else { return x; } to return original data if x.type is not what you expected.
I have an API End point that i am trying to assign variables to, now the one JSON data is an array and I Loop over it to get the data out in my console log, the difficulty i am having is that i want to assign variables to them.
Here is my code:
const request = require('request');
request('https://fantasy.premierleague.com/api/leagues-classic/1114549/standings/?page_new_entries=1&page_standings=1&phase=1', { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
var data = body.standings.results;
data.forEach(obj => {
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} ${value}`);
});
console.log('-------------------');
});
});
and here is my JSON data:
{
"league": {
"id": 1114549,
"name": "The crew",
"created": "2020-09-11T17:36:20.083556Z",
"closed": false,
"max_entries": null,
"league_type": "x",
"scoring": "c",
"admin_entry": 3523866,
"start_event": 1,
"code_privacy": "p",
"rank": null
},
"new_entries": {
"has_next": false,
"page": 1,
"results": []
},
"standings": {
"has_next": false,
"page": 1,
"results": [
{
"id": 30771462,
"event_total": 8,
"player_name": "Mohammed Ismail",
"rank": 1,
"last_rank": 0,
"rank_sort": 1,
"total": 8,
"entry": 3808290,
"entry_name": "Moe"
}
Now I am trying to console log only the standings.result.player_name in my console log so i can use it else where, how do i do that
So my output in the console should only be "player_name": "Mohammed Ismail",
I'm not sure that i get the question, but in case if you want to get all player_name and collect it in array as example, You can do it next:
const request = require('request');
const url = 'https://fantasy.premierleague.com/api/leagues-classic/1114549/standings/?page_new_entries=1&page_standings=1&phase=1';
async function getStandings(url) {
return new Promise((resolve, reject) => {
request(
url,
{ json: true },
(err, res, body) => {
if (err) {
reject(err);
return;
}
resolve(body.standings.results);
}
);
});
}
(async () => {
const data = await getStandings(url);
// here you will receive array of stadings
console.log('data : ', data);
})();
I need help replacing data inside of a JSON file using NODE.JS. My current method adds it with the same ID which is correct. However, when the data is received back, it drops the last duplicate because it found the old value first. I think what I really need to do is select JSON element by ID. Then replace it with the new.
Here is my AJAX request:
putComment: function(commentJSON, success, error) {
$.ajax({
type: 'post',
url: 'http://localhost:8080',
data: JSON.stringify(commentJSON),
success: function(comment) {
success(comment)
},
error: error
});
},
Here's my NODE:
if (req.method == 'POST') {
req.on('data', function(chunk) {
var element = JSON.parse(chunk);
fs.readFile("comments-data.json", 'utf8', function(err, json) {
var array = JSON.parse(json);
array.push(element);
fs.writeFile("comments-data.json", JSON.stringify(array), function(err) {
if (err) {
console.log(err);
return;
}
console.log("The file was saved!");
});
});
res.end('{"msg": "success"}');
});
};
Here is the data with duplicate id's:
[
{
"id": "c1",
"parent": null,
"created": "2016-08-12T19:57:21.282Z",
"modified": "2016-08-12T19:57:21.282Z",
"content": "test",
"fullname": "John Clark",
"profile_picture_url": "https://viima-app.s3.amazonaws.com/media/user_profiles/user-icon.png",
"created_by_current_user": true,
"upvote_count": 0,
"user_has_upvoted": false
},
{
"id": "c1",
"parent": null,
"created": "2016-08-12T19:57:21.282Z",
"modified": 1471031853696,
"content": "test 123",
"fullname": "John Clark",
"profile_picture_url": "https://viima-app.s3.amazonaws.com/media/user_profiles/user-icon.png",
"created_by_current_user": true,
"upvote_count": 0,
"user_has_upvoted": false
}
]
Are you just trying to replace the item if it exists? If so, you could do something like this:
var array = JSON.parse(json);
var isNew = true;
for (var i = 0; i < array.length; i++) {
if (array[i].id === element.id) {
array[i] = element;
isNew = false;
break;
}
}
//doesn't exist yet
if (isNew) {
array.push(element);
}
fs.writeFile("comments-data.json", JSON.stringify(array), function(err) {
if (err) {
console.log(err);
return;
}
console.log("The file was saved!");
});
I have a JSON file with a list of categories:
"data": {
"categories": [
{
"id": 1,
"name": "Clothes",
"children": [
"Womens",
"Mens",
"Children",
"Baby",
]
},
{
"id": "13",
"name": "Womens",
"children": [
"Womens Tops",
"Womens Bottoms",
"Womens Accessories",
]
},
{
"id": "33",
"name": "Womens Tops",
"children": []
},
In the code below I tried/failed to iterate through each node and its children to build a path variable to also be stored with the data in the mongo db:
for(var i in obj.data.categories) {
var newCat = {
name: obj.data.categories[i].name,
children: obj.data.categories[i].children
};
//UPDATE OR CREATE
Category.findOneAndUpdate({name:obj.data.categories[i].name},newCat,{upsert: true},
function(err,cat) {
if(err) { return handleError(res, err); }
if(cat.children) {
//BUILD CHILDREN PATHS
for(var j=0;j<cat.children.length;j++) {
var newChildCat = {
name: cat.children[j],
};
newChildCat.path = cat.path ? cat.path+','+cat.name : cat.name;
Category.findOneAndUpdate({ name: newChildCat.name},newChildCat,{upsert: true},
function(childErr, newChildCat) {
if(childErr) { return handleError(res, childErr); }
}
);
}
}
}
);
}
However, because javascript runs asynchronously, some node paths are being stored before their parent has been stored.
I am still kind of a newb at this and am looking for the proper/best practice way to handle asynchronous importing of objects that are dependent upon the previous creation of one another like above.
Use async module to do this. The function you need is async.eachSeries
async.eachSeries(obj.data.categories, function(_cat, cb){
var newCat = {...};
Category.findOneAndUpdate({name:_cat.name},newCat,{upsert: true}, function(err,cat) {
if(cat.children) {
async.eachSeries(cat.children, function(_cat2, cb2){
// the code like above with cb2()
}, cb);
} else {
cb()
}
});
});
I have a JavaScript function that makes an ajax call to an API, and gets a JSON array.
Here is a sample of an array that I get:
[
{
"ErrorType": "Errors",
"Explanations": [
{
"Explanation": "Price Missing",
"Locations": [
25,
45
]
},
{
"Explanation": "Unit of measurement not valid",
"Locations": [
25,
301,
302
]
}
]
},
{
"ErrorType": "Warnings",
"Explanations": [
{
Blablabla,
Ithinkthere's already much here
}
]
}
]
I put it into a JavaScript array:
$scope.CorrectionDatas = ResponseFromApi;
So, for each ErrorType, I have some "explanations". I would like to add another property, in order to have something like this :
[
{
"ErrorType": "Errors",
"Explanations": [
{
"Explanation": "Price Missing",
"Locations": [
25,
45
]
},
{
"Explanation": "Unit of measurement not valid",
"Locations": [
25,
301,
302
]
}
],
"show": true
},
{
"ErrorType": "Warnings",
"Explanations": [
{
Blablabla,
Ithinkthere's already much here
}
],
"show":true
}
]
I though that I could do it only by doing it like that:
$scope.CorrectionDatas.forEach(function (error) {
error.push({ show: true });
});
But my debugger gives me an error:
Error: error.push is not a function
$scope.getErrors/</<#http://localhost:1771/dependencies/local/js/Correction/CorrectionCtrl.js:26
Each error is an object so it don't have push, the code should be:
$scope.CorrectionDatas.forEach(function(error) {
error.show = true;
});
Try this way:
$scope.CorrectionDatas.forEach(function (error){
error["show"] = true;
});
I believe that the problem you're encountering is that error is not an array, it is an object. This can be confirmed by logging the output of typeof error. If this is the case, you must explicitly define the show property of the object, as so:
$scope.CorrectionDatas.forEach(function (error){
error['show'] = true; // alternatively, error.show = true;
});