How to access json object from array [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
In my code one of the promise returning the result like this
[{"success":true,"fileName":"./tmp/test.txt"}]
and I want to convert it into Json. how to convert it?
output:-{"success":true,"fileName":"./tmp/test.txt"}

You receive an array containing one object.
Just access array's first element, like :
var array = [{"success":true,"fileName":"./tmp/test.txt"}];
var fileName = array[0].fileName;

Related

how to access dynamic key and value of json object [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Access nested JSON object without specifying key in JavaScript
(1 answer)
How do I loop through or enumerate a JavaScript object?
(48 answers)
How to access a javascript object value without knowing the key [duplicate]
(2 answers)
Closed 21 days ago.
"value": "{"urla":{
"url1":"url2",
"url3":"url4",....}}"
how can i access urla, url1 and url2...from json object. how to access dynamic key and value of json object using value.
This urla, url1, url2 , url3 values are dynamic.
we need to access them.
in what way can we access each of them.

how to get value from list javescript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
I'm trying to extract a value with a specified key. below are the data example;
example = [{'a':0,'b':2,'c':4}]
"results I want : 2"
To get the value of b, you would need to use example[0]['b']. You need the [0] since you also have an array enclosing the object.

How to get the keyword of a json varible? [duplicate]

This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?
You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);

how to access a JSON element when i have a value of another element in the same object? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
Assume that i have this json obj:
var person=[{"name":"joe","age":21,"class":"a"},{"name":"moe","age":22,"class":"b"}];
i want to get the age of joe using given name which is joe.
is there something like this:
var age = person.name['joe'].age
You can use Array.prototype.find to search for a specific property in an array of objects.
var person=[{"name":"joe","age":21,"class":"a"},{"name":"moe","age":22,"class":"b"}];
const joe = person.find(item => item.name === 'joe');
if(joe)
console.log(`Joe is ${joe.age} years old`);

Get values from a JavaScript array [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
Say I have an array like:
var someArray = [{"id":1,"mid":"477","mname":"StackOverflow","image":"images/merchants/so.jpg"}, {"id":2,"mid":"478","mname":"Meta","image":"images/merchants/mt.jpg"}]
How do I get say the image string images/merchants/so.jpg for example?
someArray[0].image
or
someArray[0]['image']

Categories

Resources