Convert to JSONArray angularjs [duplicate] - javascript

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 7 years ago.
I've got a json like this:
"values": [
{"name": "name1"},
{"name": "name2"},
{"name": "name3"}
]
and but now i need convert it into this:
values: ["name1", "name2", "name3"];
In angularjs (or some function in javascript). Is it possible?

You're looking for a simple map function:
json.values = json.values.map(function(valObj){ return valObj.name; });
(Obviously, for readability, you may want to rename valObj to something more descriptive of your Objects)

Related

How to convert string type value to array type in JavaScript? [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 2 years ago.
I'm using JavaScript and I want to convert string type value like this:
'[{"key": "a", "value": "aa"}, {"key": "b", "value": "bb"}]'
to array type, without '' quotes, like this:
[{"key": "a", "value": "aa"}, {"key": "b", "value": "bb"}].
I receive this type as an argument to a function where I have to treat the value as Array. But currently I can't push or pop items because the value is of string type.
You can use JSON.parse() to parse it into a javascript object
Assuming the string provided is a valid json array string(but it's not. I'd to fix a missing quote after value field). However you cannot do array operations in that until you convert it into a valid javascript array.
var jsonString='[{"key": "a", "value": "aa"}, {"key": "b", "value": "bb"}]';
var array=JSON.parse(jsonString);
array.push({key:'c',value:'cc'});
We can parse the data with JSON.parse(), and the data becomes a JavaScript object.
Make sure the text is written in JSON format, or else you will get a syntax error.
When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object.
Example:
var string = '[{"key": "a", "value": "aa"}, {"key": "b", "value": "bb"}]';
var arr = JSON.parse(string)
In this case in "arr" variable we will be getting 2 objects in array

Javascript get value based on dynamic key in object [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I'm not able to get the value by dynamic key in following code,
const data = {
"Id": "1234",
"status": "open",
"Translations": {
"EN": "English",
"ES": "Spanish",
"FR": "French"
}
};
const translationKey = "FR";
console.log(data.Translations.translationKey)
How can I get "French" text from the object, If translationKey is dynamic ?
Any help is much appreciated.
Use bracket notation if you want to use a variable property name data.Translations[translationKey]. Here you will find more details.
It's the same notation one uses for accessing array elements, since an array is also an object.

Insert JSON object using jsonpath in javascript? [duplicate]

This question already has answers here:
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 4 years ago.
Is there any way to insert a JSON object in the middle of another JSON object using jsonPath? I'm using Javascript in the Postman test script after I recieve a JSON object as a response, and I need to insert JSON data in the middle of the response. I can access the part of the response I want using "$..decision[0]". Is there a way to add data using "$..decision[1]" so that this:
...
"decision": [
{
"var1": 43,
"var2": 1,
}
],...
becomes this:
...
"decision": [
{
"var1": 43,
"var2": 1
},
{
"foo1": "true",
"foo2": "false"
}
],...
If I can't, is there another simple way to append data into the middle of a JSON object?
There is nothing like "JSON object" in javascript. You have Javascript objects/arrays, arrays of objects or string containing "Java Script Object Notation".
You can use JSON.stringify() and JSON.parse() to get from one to another.
"decision" in your example will be array after parsing JSON string by JSON.parse().
You can add object into middle of an array using its method splice().
Example:
var jsonstring = '...';
var obj = JSON.parse(jsonstring);
obj.decision.splice(1,0,{ "foo1": "true", "foo2": "false"});
jsonstring = JSON.stringify($obj);

create JSON object for jsTree from XML data [duplicate]

This question already has an answer here:
Why is my Array behaving differently outside of an AJAX function? (populating jsTree) [duplicate]
(1 answer)
Closed 5 years ago.
I can't populate jsTree because something is wrong with the array that I'm creating.
jsTree allows you to pass in JSON data, so I'm trying to format an array of objects that jsTree will like:
var myAry = [];
$(xml).find('group').each(function() {
myAry.push({
"id": $(this).find('GroupID').text(),
"parent": "#",
"text": $(this).find('GroupName').text(),
});
});
When I dump [myAry] to the console, it looks like a properly formatted Array, but jsTree doesn't like it. However, if I create an array manually, jsTree likes it:
var testAry = [
{"id": "42", "parent": "#", "text": "Foo"},
{"id": "69", "parent": "#", "text": "Bar"},
{"id": "1", "parent": "#", "text": "Dolphin"},
];
What's going wrong in my loop?
This may help (from jQuery docs):
The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object.
The method you're using accepts a selector argument, but you're passing it the name of a JSON object.
Try using $.each().

Accessing object of objects - JSON API [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I'm wonder how to properly access to object of objects.I have JSON API, and I have to get some data from It.
FYI I'm using VueJS but I think that's not important thing here.
{
"name": "Foo Bar",
"details": {
"color": "black",
"slug": "foobar",
"author": "John",
"material": "Plastic"
}
}
How could I access for e.g to the slug ? Those data are stored into parent object called product (VueJS Dev Tools)
var slug = product.details.slug

Categories