This question already has answers here:
Parsing "relaxed" JSON without eval
(6 answers)
Safely parsing a JSON string with unquoted keys
(5 answers)
Closed 2 years ago.
Legs: [{
'dollar': {
xmlns: 'HitchHiker.FlightAPI.FareRequestStructs'
},
FareRequestLeg: [{
ArrivalSearchRadius: ['0'],
ArrivalTime: [{
'dollar': {
'xsi:nil': 'true'
}
}],
Class: ['Economy'],
Connections: [''],
DepartureDate: ['2020-10-22T00:00:00'],
DepartureSearchRadius: ['0'],
DepartureTime: [{
'dollar': {
'xsi:nil': 'true'
}
}],
}]
}]
I have some thing like that.
I had a xml file. I use Xml2js for parsing , here i got a this string
I want Object from of this.
clear view : https://pastebin.com/ZvPAbdgw
Use something like https://jsonlint.com/ to check your json format,
I fixed few lines for you
{
"Legs": [{
"dollar": {
"xmlns": "HitchHiker.FlightAPI.FareRequestStructs"
}
}]
}
then you could assign it to a variable.
const json = {
"Legs": [{
"dollar": {
"xmlns": "HitchHiker.FlightAPI.FareRequestStructs"
}
}]
}
If the format returned is string you would need to do
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
const jsonObj = JSON.parse("your json string")
Related
This question already has answers here:
Using bracket notation opposed to eval
(1 answer)
Convert a string to a variable name in javascript?
(3 answers)
Closed 1 year ago.
I have data like this
{
"data": {
"x": [
{
"id": 1,
"y": "yData1"
},
{
"id": 2,
"y": "yData2"
}
],
"xx": {
"xxx": [
{
"id": 1,
"yyy": "yyyData1"
},
{
"id": 2,
"yyy": "yyyData2"
}
]
}
}
}
sometime i just want object x or xx or xxx or many sub children in datas, i ready using eval for filter by using object name to return data
filter(data: any, key: any) {
return eval("data." + key)
}
any idea ?? because eval in typescript/javascript not recommended
Just use bracket notation in order to use variable values as key:
filter(data: any, key: any) {
return data[key];
}
This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 2 years ago.
A document contains an array of object each object contains a key i want to find all the object that matches the key in mongoose.
Below is the sample schema:
const TalentSchema = new mongoose.Schema({
name: String,
dev_task: [
{
development_training: {
type: mongoose.Schema.Types.ObjectId,
ref: 'TrainingModule',
},
developlink: String,
},
],
})
I want to match the document that have development_training to given Id.
How can i do that ?
Update :
Sample Data
[
{
"name": "name1",
"dev_task": [
{
"development_training": 1,
"developlink": ""
},
{
"development_training": 2,
"developlink": ""
}
]
},
{
"name": "name2",
"dev_task": [
{
"development_training": 1,
"developlink": ""
}
]
},
]
It should return This : -
[
{
"_id": ObjectId("5a934e000102030405000000"),
"dev_task": [
{
"developlink": "",
"development_training": 1
}
],
"name": "name1"
},
{
"_id": ObjectId("5a934e000102030405000001"),
"dev_task": [
{
"developlink": "",
"development_training": 1
}
],
"name": "name2"
}
]
As explained into docs, mongo uses dot notation.
MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.
And this is exactly we want.
So to access your document you need to do dev_task.development_training into find query in this way:
db.collection.find({
"dev_task.development_training": 1
})
Example here
Note that using mongoose is the same query:
yourModel.findOne({"dev_task.development_training": yourId}).then(result => {/*...*/})
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
How will you access symbol property here in JS
console.log(hi[0]) outputs { symbol: "asdf" , name:"adad"}
but
console.log(hi[0].symbol) throws the error that symbol is undefined
hi is an array. First you need to access the object so hi[0] will provide the first object. Then access the object using the key which is 0 like below
const hi = [{
0: {
symbol: "asdf",
name: "adad"
}
}];
console.log(hi[0][0].symbol)
hi variable is an Array so you have to access the first element of the array first, then the Object property:
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
console.log(hi[0][0].symbol)
Like this?
console.log(hi[0][0].symbol)
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
hi[0][0].symbol is right.
console.log(hi[0]) => 0: {symbol: "asdf", name: "adad"}
console.log(hi[0][0]) => {symbol: "asdf", name: "adad"}
console.log(hi[0][0].symbol) => "asdf"
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I need to traverse a JavaScript object with a stored string.
Sample string
var x = "Desserts"
Sample Object
{
"dataset":
{
"Categories" :
[
{
"Desserts" :
[
"Sweets","Ice Creams","Pastry"
]
} ,
{
"Juices and Beverages" :
[
"Cold","Hot","Fresh","Sodas"
]
}
}
}
If I traverse the object as dataset.Categories.x, it doesn't work[returns undefined] . How can I do this?
You should use dataset.Categories[0][x] instead of dataset.Categories[0].x
Take a look at : dot notation or the bracket notation
var x = "Desserts",
data = {
"dataset": {
"Categories": [{
"Desserts": ["Sweets", "Ice Creams", "Pastry"]
}, {
"Juices and Beverages": ["Cold", "Hot", "Fresh", "Sodas"]
}]
}
}
alert(data.dataset.Categories[0][x]);
var obj = {
"dataset": {
"Categories": [{
"Desserts": ["Sweets", "Ice Creams", "Pastry"]
}, {
"Juices and Beverages": ["Cold", "Hot", "Fresh", "Sodas"]
}]
}
}
var x = 'Desserts';
var val = obj.dataset.Categories[0][x];
console.log(val);
JSFIDDLE.
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I am creating a list by fetching values from a JSON file. It is a nested JSON and the list items are- "Thriller","Fiction" which are basically keys for the next level.
on click of the item I'm passing its name(i.e. Thriller/fiction) to another function...
var val = thriller.
Now I need to fetch the value(i.e. "book" & bookname) corresponding to the passed key in this new function. I'm not able to do so using dot operator-
data.library.val not working..
If anybody has worked on something similar..please help..
JSON:
{ "library": [
{
"Thriller": [
{ "book": "ABC" },
{ "book": "DEF" }
]
},
{
"Fiction": [
{ "book": "GHI" },
{ "book": "JKL" }
]
},] }
Code snippet:
$.getJSON('resources/abc.json', function(data){
var i = data.library;
$("#menuList1").css('display','block');
$(i).each(function(key, value){
$.each(value, function(key, value){
console.log(key);
$("#menuList1").append(''+key+'');
});
}); });
Use data.library[key]
MDN Documentation
Your Json is not valid, this },] specially. A good version :
{
"library": [{
"Thriller": [{
"book": "ABC"
}, {
"book": "DEF"
}]
}, {
"Fiction": [{
"book": "GHI"
}, {
"book": "JKL"
}]
}]
}
you can refer the website http://jsonlint.com for validating your json.