Javascript acces json path with additional characters [duplicate] - javascript

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
I have a JSON Structure like this:
{
"SOAP: Envelope": {
"SOAP: Header": "",
"xmlns: SOAP": "http: //schemas.xmlsoap.org/soap/envelope/",
"SOAP: Body": {
"ns0: Z_ZBC_USAGE_GET_DATA.Response": {
"IT_AREA_RANGE": "",
"ET_USAGE": {
"item": [...]
}
}
}
}
}
In my JS I try to access to the items
reports.data = data.SOAP:Envelope.SOAP:Body.ns0:Z_ZBC_USAGE_GET_DATA.Response.ET_USAGE.item;
This didn't worked because : signs are not allowed. What should be the correct expression to get the items?

Just access properties like array keys:
reports.data = data['SOAP:Envelope']['SOAP:Body']['ns0:Z_ZBC_USAGE_GET_DATA.Response'].ET_USAGE.item;

Related

Is there a way to access a JSON value multi-level deep in one call in jQuery/Javascript? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 1 year ago.
If I have a multi-level json object like this:
{
employee : {
name : "Blah",
company : {
name : "Company"
}
}
}
I know I can access company name like employee.company.name, but is there a way to do this dynamically? Like I tried employee["company.name"] But that doesn't work. Basically if I have the string "company.name" I want to get that value in one call.
You can do this by accessing each sub-object separately:
employee["company"]["name"]
var data = {
employee : {
name : "Blah",
company : {
name : "Company"
}
}
};
console.log(data["employee"]["company"]["name"]);

Getting random element from constants that they have subs [duplicate]

This question already has answers here:
Pick random property from a Javascript object
(9 answers)
Closed 2 years ago.
const example1 = {
subex1: {
...something...
},
subex2: {
...something...
}
};
This is the code. For some reason, I can't use example1[Math.floor(Math.random() * example1.length)] code. How can I get a random item from sub constants (actually idk their name if they're not subconst).
You can do something like this.
const example1 = {
subex1: {
"hello": "hello"
},
subex2: {
"Hello1": "hello1"
}
};
console.log(Object.keys(example1)[Math.floor(Math.random()*Object.keys(example1).length)]);

JavaScript positional property access in Objects [duplicate]

This question already has answers here:
How to access the first property of a Javascript object?
(23 answers)
Accessing a JavaScript's object property without knowing that property name
(3 answers)
JavaScript: Get first and only property name of object
(7 answers)
How do I access properties of a javascript object if I don't know the names?
(8 answers)
Closed 3 years ago.
I have the following JavaScript object:
{
"_embedded": {
"dealerListItemDToes": [
{
...
},
{
...
}
]
}
}
Property called 'dealerListItemDToes' will always be at the given position in the object but its name can vary depending on the HTTP requests.
How can I access the property 'dealerListItemDToes' and retrieve its content without referencing its name?
Since it's the only property of the _embedded object, you could access the [0]th item in an array of object entries:
const obj = {
"_embedded": {
"dealerListItemDToes": [
{
// ...
},
{
// ...
}
]
}
};
console.log(
Object.entries(obj._embedded)[0]
);
You can try like this
let data = {
"_embedded": {
"dealerListItemDToes": [{
"h": 1
}]
}
}
console.log(data._embedded[Object.keys(data._embedded)[0]])

Not able to get JSON value dynamically [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I am working on the following snippet. Why am I not able to get the
let node = 'Em2';
console.log(data.node.c2);
work? As you can see I am able to get data while passing data.Em2.c1 but a dynamic format like this let node = 'Em2'; console.log(data.node.c2); I am getting this error
TypeError: Cannot read property 'c2' of undefined
Code:
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
Use square braces [] to access object member via variable
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data[node].c2);
Similar question: how to access object property using variable
Instead of console.log(data.node.c2), try console.log(data[node][c2])

Javascript - Using Object literal with a number [duplicate]

This question already has answers here:
Using integer keys with dot notation to access property in javascript objects
(3 answers)
Closed 6 years ago.
Let's say I have an object:
var elObject = {
one: {
name: "Oliver"}
}
I can access name by doing elObject.one.name and everything is great, but let's say I have this instead:
var elObject = {
1: {
name: "Oliver"}
}
Suddenly, I can't access name through elObject.1.name anymore since I'm using 1 instead of 'one'. Is there a a special escape or something I'm supposed to use with object literal and digits?
You can declare the plain object with number 1 using it as string. Once is not allowed to have a property name starting with number, you can access it using bracket notation.
Example and findle below.
var x = {
'1' : {
name: 'Joao'
}
};
alert(x);
try {
alert(x['1'].name);
}
catch(e){
alert(e.message);
}
https://jsfiddle.net/b4c34wLv/

Categories

Resources