create JSON object for jsTree from XML data [duplicate] - javascript

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().

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

pushing array of objects into another array of objects [duplicate]

This question already has answers here:
JavaScript: How to join / combine two arrays to concatenate into one array?
(3 answers)
Closed 4 years ago.
I have a newbie question regarding arrays and objects. I have an array of objects that I would like to push into another array of objects, but can't seem to get it to work correctly.
For instance, if I have data.list:
data.list=[{
a,
b,
c
}];
and I want to push data.list into another object array called data.overall so that it looks like this:
data.overall=[{
data.list,
z,
y,
x
}];
Sorry, for clarification, I want data.list to still exist as an array within data.overall. I've tried concat, but I keep getting an error in the console saying Server JavaScript error Cannot find function concat in object [object Object].
Thanks!
As you said, newbie I am trying to help you,
Javascript Objects should have always properties like below and you wanted to achieve this? Let me know if you have any questions.
From your question, I assume that you wanted to achieve this, Hope this helps to proceed next with Javascript
And also you can always check how JSON should be structured with below URL
https://jsoneditoronline.org/
var data = [{
"a": 1,
"b": 2,
"c": 3
}];
var dataNew = [{
"x": 4,
"y": 5,
"z": 6
}]
function getData(data) {
return data;
}
var modiFiedObject = [{
"data": getData(data[0]),
"dataNew": getData(dataNew[0])
}]
console.log(modiFiedObject);

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);

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

Convert to JSONArray angularjs [duplicate]

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)

Categories