Extracting specific value from json response [duplicate] - javascript

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I add a json value from a response to a variable and then print it this way result: ${JSON.stringify(result)}, but what i want exactly is to get only one exact value from this json
here's the json
{
"flags": [
{ "flagID": "0f0571b5-af7b-48e2-8418-044cbec0166d", "flagname": "lionsflag", "team": "lions", "membersnum": "4", "win": false },
{ "flags": "9819c0bd-e134-4950-a1ee-7f89450ee4b6", "flagname": "Barcaflag", "membersnum": "4", "win": true }
]
}
I only want to extract lionsflag string only from the first flagname, this value is able to change any time

You need to use Object.keys() to get all the keys and then check the related value is meet your demand or not
let data = {
"flags": [
{ "flagID": "0f0571b5-af7b-48e2-8418-044cbec0166d", "flagname": "lionsflag", "team": "lions", "membersnum": "4", "win": false },
{ "flags": "9819c0bd-e134-4950-a1ee-7f89450ee4b6", "flagname": "Barcaflag", "membersnum": "4", "win": true }
]
}
let value = "lionsflag"
data.flags.filter(d => Object.keys(d).forEach(e =>{
if(d[e] == value){
console.log(e + ";" + value)
}
}))
let prop ='flagname'
console.log(data.flags[0][prop])

Related

JavaScript Stringify() applies different rules to the key on the same object [duplicate]

This question already has answers here:
JSON.stringify: how to skip indentation for one (or more) objects
(1 answer)
Json - stringify so that arrays are on one line
(10 answers)
JSON.stringify custom formatting
(3 answers)
Closed 3 months ago.
I am working on a JSON file generator while everything works fine the final result is not as good as expected
With following stringify function call, following json file would be generated.
var jsonString = JSON.stringify(object, null, 2);
{
"key_1": 2397,
"key_2": "text1",
"key_3": "text",
"key_4": [
"12345",
"23456"
],
"key_5": [
0,
1
],
"key_6": [
0
],
"key_7": [
"https://xxxx",
"https://xxxx"
],
"key_8": "12345",
}
In the JSON file I want to make key_5 and key_6 skip the stringify attributes and make it like this
"key_5": [0,1],
"key_6": [0],
So the final file would look like this
{
"key_1": 2397,
"key_2": "text1",
"key_3": "text",
"key_4": [
"12345",
"23456"
],
"key_5": [0,1],
"key_6": [0],
"key_7": [
"https://xxxx",
"https://xxxx"
],
"key_8": "12345",
}
I think it is possible to customized the rule for certain key but I am very new to JS so I am not sure how to do so...
jsonString = JSON.stringify(object, (key, value) => {
if (key === "key_5"||key === "key_6") return Do_something_ELSE;
return value;
}, 2);

how can I retrieve specific property of an object of multiple objects [duplicate]

This question already has answers here:
Perform .join on value in array of objects
(14 answers)
Closed 1 year ago.
I need help in getting values of specific property of an array of multiple objects
I have an array like this:
[
{
"id": 1,
"name": "skill 1"
},
{
"id": 2,
"name": "skill 2"
},
{
"id": 3,
"name": "skill 3"
}]
and I want to get a string like this :
skill 1 - skill 2 - skill 3
First step is to extract name. You can use map.
elements.map(x=>x.name)
Second step is to join it to one string using join
const result = elements.map(x=>x.name).join(' - ');

How to efficiently remove nameless attribute from json object js [duplicate]

This question already has answers here:
How do I remove all null and empty string values from an object? [duplicate]
(13 answers)
Closed 1 year ago.
I have a very large array of objects and some of the objects have a nameless property. What is an efficient way to remove the property?
[
...
{
"phone_number": "***-***-****",
"category": "Abc",
"link_to_company": "www.example.com",
"email": "test#test.com",
"company_name": "test company",
"": "123", // this need to be removed
"status": 1
},
...
]
A map function should do it.
array.map(v => { delete v[""]; return v })

Why Json.parse keeps changing my interger value in a nested object [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Closed 6 years ago.
my obj = {
"formname": ["appname", {
"operation": ["add", {
"values": {
"Activation_code": "12345",
"ID": 722756000010586033,
"game_Id": "10000"
},
"status": "Success"
}]
}]
}
The above code is my object from a server I have no problem getting the value the point am making is why JSON.parse(obj) is changing the ID after the object as gone through its function.
hence after a parse it spits back the ID as 722756000010586000 Why?
Cos of number precision, your id is way past max safe integer, I suggest keeping it as string.
console.log(722756000010586033 > Number.MAX_SAFE_INTEGER)
console.log(722756000010586033);
Basically there is some finite memory used to store numbers in JS and with finite momory comes finite number of digits that can be remember. This is ofc a little bit more complicated than that, but that is basically it.
You can extract your id with regular expression:
const JSON = `{
"formname": ["appname", {
"operation": ["add", {
"values": {
"Activation_code": "12345",
"ID": 722756000010586033,
"game_Id": "10000"
},
"status": "Success"
}]
}]
}`;
const matched = JSON.match(/"ID":\s*(\d+),/);
const id = matched[1];
document.body.innerHTML = id;

How to get last item from array of object? [duplicate]

This question already has answers here:
Get the last item in an array
(59 answers)
Closed 6 years ago.
I have JSON object from server and requirement is to always display last item from the array items. how can i achieve that task using AngularJs or native JavaScript ?
Below case i have to display text Chief Administrative Officer.
main.js
angular.forEach($scope.rcsaErhTreeData, function(val) {
angular.forEach(val, function(val) {
console.log('this is the array value', val[0].text);
});
});
json.js
[{
"uid": null,
"index": 0,
"selected": null,
"expanded": null,
"id": 2701,
"text": "BAC Enterprise Wide",
"parentId": 0,
"items": [{
"uid": null,
"index": 0,
"selected": null,
"expanded": null,
"id": 4114,
"text": "Chief Administrative Officer",
"parentId": 2701,
"items": []
}]
}]
if you try the following code in your context you will get the result.
console.log(JSON.stringify(items[items.length - 1]));
The console will show you the stringified output...
Thanks,
Paras
I am not a Angular.js programmer, but the concept would be to:
1. Either do it in a loop and overwrite the variable value with the next value, that way, the last one will persist. -- Not recommended due to performance issues, and using a loop when it can be done without a loop.
Pseudo code:
var outVal='';
angular.forEach($scope.rcsaErhTreeData, function(val) {
angular.forEach(val, function(val) {
outVal = val[0].text);
});
});
console.log('this is the last item value', outVal);
Another approach would be find the length of the val array, and go to its last index to fetch its value. I am not a pro in this language so cannot give an example code. Sorry about that.

Categories

Resources