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"]);
Getting value from JSON, which has space in key name using JavaScript
There are two ways of accessing properties;
foo.bar
foo['bar']
you need to use the second option to use spaces for example foo['bar bar'].baz
How to access Json Object which has space in its name?
var json = /* your JSON object */ ;
json["Object"]["PET ANIMALS"].length // returns the number of array items
Using a loop to print the number of items in the arrays:
var obj = json["Object"];
for (var o in obj) {
if (obj.hasOwnProperty(o)) {
alert("'" + o + "' has " + obj[o].length + " items.");
}
}
Parsing Json response using PHP with space in array key
You need to put them inside a curly brace with a single quote:
$place_name = $response->places[0]->{'place name'};
echo $place_name;
Or as @scragar said in the comments, if you're not confortable accessing them thru objects, you can put a true
flag on json_decode($response, true)
, so that you can access them as associative arrays instead.
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)"]
How to read field name with space in Json using OPENJSON in SQL Server 2016
Generally it is a bad idea to use spaces in the attribute name.
I would leave out the [
]
from your OPENJSON name and varchar(60) - source MSDN OPENJSON.
Now to actually answer your question:
You need to format your attribute with double quotes in the WITH
clause:
@DECLARE @json NVARCHAR(MAX);
SET @json=N'{ "full name" : "Jayesh Tank"}';
SELECT * FROM OPENJSON(@json) WITH (name varchar(60) '$."full name"')
for the second one:
SET @json = N'{ "name " : "abc"}';
SELECT * FROM OPENJSON(@json) WITH ( name varchar(60)'$."name "')
In JavaScript/jQuery, how to retrieve data that has spaces in its name?
Array member access notation works on objects as well.
$.getJSON(url, null, function(objData) {
$.each(objData.data, function(i, item) {
var zip = item.Zip;
var fname = item['First Name'];
});
});
You can use this for arbitrary strings (those that aren't legal identifiers) as well as variables.
var fieldName = "First Name";
var fname = item[fieldName];
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'
}
Related Topics
How to Change Background Opacity When Bootstrap Modal Is Open
Reset Form Input Values in React
Handle Browser Close in JavaScript
Programmatically Clicking Button in React Native
Angular: Toggle Active Class on Only Button the Current Clicked Button (Not Using *Ngfor)
Array Push Is Overwriting the Existing Array
Puppeteer Wait Until Page Is Completely Loaded
Jquery - Get Id Dynamically from Generated Element
How to Block +,-,E in Input Type Number
How to Detect If Two Divs Are Touching/ Collision Detection
Use Lodash to Find Objects in an Array Matches an Id (Complex, Drilldown)
Javascript - Remove Milliseconds from Date Object
Validate Phone Number With Yup
Ios Safari/Chrome - Unwanted Scrolling When Focusing an Input Inside the Modal
How to Clear Location.State in React-Router on Page Reload
Merge Array of Objects by Same Key-Value
How to Convert from Google Sheet Date Number Value to JavaScript Date