Accessing JSON object keys having spaces
The way to do this is via the bracket notation.
var test = { "id": "109", "No. of interfaces": "4"}alert(test["No. of interfaces"]);
How to access json object key with space
Change
console.log(data[key].Per Mertesacker);
toconsole.log(data[key]['Per Mertesacker']);
Read Working with objects Getting value from JSON, which has space in key name using JavaScript
There are two ways of accessing properties;
foo.bar
foo['bar']
foo['bar bar'].baz
How to deal with spaces in JSON Key?
Wrap it with '
:
import groovy.json.JsonSlurper
def input = '''{
"total": 3,
"page": 1,
"totalPages": 1,
"results": [{
"person name": "John Doe",
"date of birth": "01/01/1990",
"date of registration": "01/01/2016",
"notes": "default user",
}]
}'''
def json = new JsonSlurper().parseText(input)
json.results.each { res ->
assert res.'person name' == 'John Doe'
}
json object has key with spaces and parenthesis
The key Member ID (U1)
contains a ZERO WIDTH NO-BREAK SPACE' (U+FEFF) so when you try to access it without that invisible character then it is undefined. You can access the key like this:
var member_id = transaction["\uFEFFMember ID (U1)"]
angularJS - Accessing JSON keys that have spaces in them
How about
task['CAR ID']
No period needed between task and the opening bracket. how can i access the object properties that have spaces
You can access using bracket notation
data['personal details']
same for all the other keys with spaces as well as with a single word data['personal details']['name']
but it's better to use .
dot notation for single word json keysdata['personal details'].name // "Loren"
data.address['temporary address'] // prints "Acn Block Ist Phase"
How can I access a JavaScript object which has spaces in the object's key?
Use ECMAscripts "bracket notation":
myTextOptions[ 'character names' ].kid;
You can use that notation either way, reading & writting.For more information read out here:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Accessing JSON key with spaces in a foreach
You can do it this way
$json->{'Realtime Currency Exchange Rate'}
Hope that helpsUpdate:
$data = array(
"Realtime Currency Exchange Rate" => array(
0 => array(
"1. From_Currency Code" => "EUR",
"2. From_Currency Name" => "Euro",
"3. To_Currency Code" => "USD",
"4. To_Currency Name"=> "United States Dollar",
"5. Exchange Rate" => "1.14122552",
"6. Last Refreshed" => "2018-08-14 05:07:51",
"7. Time Zone" => "UTC"
),
1 => array(
"1. From_Currency Code" => "USD",
"2. From_Currency Name" => "United States Dollar",
"3. To_Currency Code" => "EUR",
"4. To_Currency Name" => "Euro",
"5. Exchange Rate" => "0.87692400",
"6. Last Refreshed" => "2018-08-14 05:09:17",
"7. Time Zone" => "UTC"
)
)
);
$data = json_encode($data);
$json = json_decode($data);
$json_response = array();
foreach ($json->{'Realtime Currency Exchange Rate'} as $row) {
echo "<pre>";
print_r($row);
echo "</pre>";
}
Formatted JSON string will look like this{
"Realtime Currency Exchange Rate": [{
"1. From_Currency Code": "EUR",
"2. From_Currency Name": "Euro",
"3. To_Currency Code": "USD",
"4. To_Currency Name": "United States Dollar",
"5. Exchange Rate": "1.14122552",
"6. Last Refreshed": "2018-08-14 05:07:51",
"7. Time Zone": "UTC"
}, {
"1. From_Currency Code": "USD",
"2. From_Currency Name": "United States Dollar",
"3. To_Currency Code": "EUR",
"4. To_Currency Name": "Euro",
"5. Exchange Rate": "0.87692400",
"6. Last Refreshed": "2018-08-14 05:09:17",
"7. Time Zone": "UTC"
}]
}
Related Topics
React Jsx: Selecting "Selected" on Selected <Select> Option
Mocking a Useragent in JavaScript
Angular 2 Router Event Listener
Catching Errors in JavaScript Promises with a First Level Try ... Catch
Why Do Empty JavaScript Arrays Evaluate to True in Conditional Structures
React Routing Works in Local MAChine But Not Heroku
Js/Es6: Destructuring of Undefined
Properties of JavaScript Function Objects
Understanding Xmlhttprequest Over Cors (Responsetext)
Javascript: Cancel/Stop Image Requests
Initializing Select with Angularjs and Ng-Repeat
How to Pass JavaScript Variables as Parameters to Jsf Action Method
What Are Some Empirical Technical Reasons Not to Use Jquery
Object Doesn't Support This Property or Method Rails Windows 64Bit
Why Does JavaScript's Regex.Exec() Not Always Return the Same Value
Npm Start Error with Create-React-App
What Is the JavaScript Mime Type for the Type Attribute of a Script Tag