How to read JSON with multiple key value pair [duplicate] - javascript

This question already has answers here:
How to parse JSON data with jQuery / JavaScript?
(11 answers)
Closed 4 years ago.
On AJAX request response is following :
[ {"status":{"login":"invalid"}},
{"user":{"username":false,"pwd":false}}
]
How to read values in Jquery.

Use JSON.parse(string) to get an Object.
After that you can access values as default:
var json = JSON.parse(res.data)
console.log(json.status.login)

Related

How to loop through json response in javascript? [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Why is using "for...in" for array iteration a bad idea?
(28 answers)
Loop through an array in JavaScript
(46 answers)
JavaScript loop through JSON array?
(14 answers)
Closed 2 years ago.
Note: I have already gone through stackoverflow similar questions but without any luck.
I have following json response received in ajax. I am trying to loop through it but still no success.
[
{"id":"1","type":"size","value":"large","datetime":"2020-10-20 13:45:49"},
{"id":"14","type":"color","value":"red","datetime":"2020-10-20 13:45:49"}
]
I tried many ways including following code but could not get desired result.
for( let prop in responce ){
console.log( responce[prop].id );
}
for( let prop in responce ){
console.log( responce[prop] );
}
Kindly advise how to resolve it?

Add a new attribute to json object [duplicate]

This question already has answers here:
Add new attribute (element) to JSON object using JavaScript
(11 answers)
Add property to each object in the array [duplicate]
(2 answers)
Closed 6 years ago.
I have a JSON object of the following format
[{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"},
{......},
{......},
]
I need to add a new attribute to each row based on some calculations.
Expected result
[{"field1":1,"field2":"A","field3":"B","field4"="generatedVal1"},
{"field1":2,"field2":"B","field3":"C","field4"="generatedVal2"},
{......},
{......},
]
How can I achieve this using javascript?
Use Array.prototype.forEach method:
[
{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"}
]
.forEach(obj => {
obj.field4 = 'Something'
})
Sidenote on terminology: you don't have any JSON, you have javascript array (object). JSON is a string representation of this object, but in your question you are talking about object itself.

How to convert string to object in Angularjs [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 6 years ago.
I have a string like :
$scope.text = '"{\"firstName\":\"John\",\"age\":454 }"';
and I want to convert to js object:
$scope.tmp = {"firstName":"John","age":454 };
Note: JSON.parse() doesn't work!!
It's my sample in codepen
You can do it with angular.fromJson()
in your sample, it would have been $scope.tmp = angular.fromJson($scope.text);
The difference between JSON.Parse() and angular.fromJson, is that angular will check to make sure a string is provided. If it is already an object, it will return the same object.

JSON - from regex to JSON [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
how do you get objects for events -> performances -> name & occurs_at to this json file https://api.myjson.com/bins/w05x using javascript. Cannot use regex since json API keeps changing format. Thanks
Use the JSON parser
obj = JSON.parse(json_string);
then you access it with
obj.events[event_index].performances[performance_index].performer.name
obj.events[event_index].name
obj.events[event_index].occurs_at
JSFiddle example of listing all the event attributes name and occurs_at.

How to get the value dynamically from JSON object [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 7 years ago.
I want to retrieve a JSON value dynamically from JSON object. Below is the code i am using to get the value from JSON object.
var jsonObj = JSON.parse(jsonData);
console.log(jsonObj);
jsonSplit = jsonToFind.split(htmlSplit+".")[1].trim();
console.log(jsonObj+"."+jsonSplit);
But I am getting [object Object].ensighten_tag.
Here ensighten_tag is the dynamic key value.
Can anyone suggest me how to get the value dynamically ?
console.log(JsonObj[jsonSplit])

Categories