Accessing JSON Object Keys Having Spaces

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

to

console.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;

  1. foo.bar
  2. foo['bar']

you need to use the second option to use spaces for example 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 keys

data['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 helps

Update:

$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



Leave a reply



Submit