How to get JSON data key names in a array format using nodejs?
I've tried the following but it returns object. But I want an array so I can store it in a varaible.
const jsondata = [{ "name": "StorageType", "value": "AllStorageTypes" }, { "name": "BucketName", "value": "testing" }]
Object.keys(jsondata).forEach(function(key) {
var value = jsondata[key];
console.log(value)
});
output:
{ name: 'StorageType', value: 'AllStorageTypes' }
{ name: 'BucketName', value: 'testing' }
Expected output:
["StorageType", "BucketName"]
Try this
const jsondata = [{ "name": "StorageType", "value": "AllStorageTypes" }, { "name": "BucketName", "value": "testing" }]
const arrayData = jsondata.map(item => item.name)
console.log(arrayData)
jsondata.map(obj=>obj.name)
Related
I am fetching API into my Express server which has several JSON key value pairs in one array.
For Example:
[{
"quality": "best",
"url": "https://someurlhere.example/?someparameters"
},
{
"quality": "medium",
"url": "https://someurlhere1.example/?someparameters"
}]
And I want to create an array of JSON of that received data in this Format:
[{
"best": "https://someurlhere.example/?someparameters"
},
{
"medium": "https://someurlhere1.example/?someparameters"
}]
I have tried doing this by using for loop
for(let i=0; i < formats.length; i++){
arr.push({
`${formats[i].quality}` : `${formats[i].url}`
})
}
But it didn't work for me.
Please help me in achieving this.
Thanks in Advance :)
You could use the map function and create a new object from it.
For example:
let prevArr = [{
"quality": "best",
"url": "https://someurlhere.example/?someparameters"
}, {
"quality": "medium",
"url": "https://someurlhere1.example/?someparameters"
}]; // Replace with your array
let newArr = [];
let obj = {};
prevArr.map(function(x) {
obj = {};
obj[x["quality"]] = x.url;
newArr.push(obj);
});
const input = [{
"quality": "best",
"url": "https://someurlhere.example/?someparameters"
}, {
"quality": "medium",
"url": "https://someurlhere1.example/?someparameters"
}];
const result = input.map((v, i) => {
return {
[v["quality"]]: v["url"]
}
});
console.log(result)
I have a json request input below and would want to construct a specific json format that the backend needs using javascript. What is the best way or steps to construct them? Any help would be appreciated! Thanks.
Request input :
{
"Product": "abc"'
"Data": "{"Name":"John","Email":"john#example.com"}"
}
Request output to the backend as follow:
{
"variables": {
"Product": {
"value": "abc",
"type": "string"
},
"Data": {
"value": "{"Name":"John","Email":"john#example.com"}",
"type": "string"
}
},
"Key": "123"
}
Thanks in advance
You can just map over all the entries in your object.
Documentation: Object.entries() Array.reduce()
const input = {
Product: "abc",
Data: "{\"Name\":\"John\",\"Email\":\"john#example.com\"}"
};
const variables = Object.entries(input).reduce((output, [key, value]) => {
output[key] = {
type: typeof value,
value
};
return output;
}, {});
const result = {
variables,
key: '123'
};
console.log(result);
How can i make json object from
{
"Public_Holiday": [{
"id": 1,
"date": "2018-12-31T15:59:14.000Z"
}]
}
Into this and add momentjs
{
"Public_Holiday": [{
"id": 1,
"date": "2019-1-1"
}]
}
And i tried to restructure json by doing
const items = await jsonfile.readFile('./json/data.json');
const objKey = Object.keys(items);
const json_struct = objKey.map((key) => {
tablename: key,
table: items[key]
})
So how can i filtered the json_struct with every object date and increment it by 1?
Given this data structure:
let assets = [{
"photos": [{
"id": 1,
"label": "bad-syn.jpg",
"size": 38284
}]
}, {
"documents": [{
"id": 109
}]
}]
]
How can I retrieve the subarray based on the photos key? There can be other keys.
My function just returns the entire structure:
findAssets: function (key) {
return this.assets.find((asset) => {
return asset[key]
})
}
If you want to return the photos sub array or in other words only the value of the given key, you just want to access the key value from the .find() result using [key]:
findAssets = function(key){
return assets.find((asset) => {
return asset[key]
})[key]
}
Demo:
let assets = [{
"photos": [{
"id": 1,
"label": "bad-syn.jpg",
"size": 38284
}]
}]
findAssets = function(key){
return assets.find((asset) => {
return asset[key]
})[key]
}
console.log(findAssets("photos"));
Note:
This assumes the given keyexists in your assets objects, otherwise it can throw an error.
You want to use .map() and because photos is an array as well you need to map twice.
let assets = [{
"photos": [{
"id": 1,
"label": "bad-syn.jpg",
"size": 38284
}]
}]
function byKey(k) {
return assets.map(a => a.photos.map(p => p[k]));
}
console.log(byKey('label'));
[{
"circlemarker": [{
"type": "circle_marker"
}, {
"latlong": "abc"
}]
}, {
"connector_marker": [{
"type": "icon_marker"
}, {
"latlong": "pqr"
}]
}, {
"icon_marker": [{
"type": "connector_marker"
}, {
"latlong": "xyz"
}]
}]
I want to access latlong values of each marker. So how can I have access to each property in this structure.
You can get latlong data:
for (var a = 0; a < obj.length; a++) {
var key = Object.keys(obj[a])[0];
var latlong = obj[a][key][1];
console.log(latlong));
}
But i think that data have not correct structure, this is better solution:
var markers = [{
"name": "circlemarker",
"type": "circle_marker"
"latlong": "abc"
}, {
"name": "connector_marker",
"type": "icon_marker",
"latlong": "pqr"
}, {
"name": "icon_marker",
"type": "connector_marker",
"latlong": "xyz"
}];
I think this should work for you:-
var makers = [{"circlemarker":[{"type":"circle_marker"},{"latlong":"abc"}]},{"connector_marker":[{"type":"icon_marker"},{"latlong":"pqr"}]},{"icon_marker":[{"type":"connector_marker"},{"latlong":"xyz"}]}];
makers.forEach(function(maker){
var makerName = Object.keys(maker)[0];
console.log(maker[makerName][1]["latlong"]);
});
so for each object in the array, you want to pluck the latlong from the first key which also references another array of objects. Man I would fix this data structure but if you can't control it, you can do this:
#!/usr/bin/env node
var data = [{
"circlemarker": [{
"type": "circle_marker"
}, {
"latlong": "abc"
}]
}, {
"connector_marker": [{
"type": "icon_marker"
}, {
"latlong": "pqr"
}]
}, {
"icon_marker": [{
"type": "connector_marker"
}, {
"latlong": "xyz"
}]
}];
var _ = require('lodash')
, coords = [];
_.each(data, function(item){
//console.log(item);
var key = _(Object.keys(item)).first()
, first = item[key]
, latLong = _.pluck(first, 'latlong')[1];
if ( latLong ) {
coords.push(latLong);
}
});
console.log(coords);
Produces the following output:
[ 'abc', 'pqr', 'xyz' ]