remove whitespace inside json object key value pair
Assign the trimmed value to the specific user[key]
:
var user = { first_name: "CSS", last_name: " H ", age: 41, website: "java2s.com" };
for (var key in user) { user[key] = user[key].toString().trim() console.log(key+"-->"+user[key]);}
Trim white spaces in both Object key and value recursively
You can clean up the property names and attributes using Object.keys to get an array of the keys, then Array.prototype.reduce to iterate over the keys and create a new object with trimmed keys and values. The function needs to be recursive so that it also trims nested Objects and Arrays.
Note that it only deals with plain Arrays and Objects, if you want to deal with other types of object, the call to reduce needs to be more sophisticated to determine the type of object (e.g. a suitably clever version of new obj.constructor()).
function trimObj(obj) {
if (!Array.isArray(obj) && typeof obj != 'object') return obj;
return Object.keys(obj).reduce(function(acc, key) {
acc[key.trim()] = typeof obj[key] == 'string'? obj[key].trim() : trimObj(obj[key]);
return acc;
}, Array.isArray(obj)? []:{});
}
Regex to remove spaces from Key but not Value
You can probably try the following expression as a quick fix granted the JSON you need to fix is in the format you have shown (it might fail to work with other JSONs):
\s(?=[^"]*":\s*")
See the regex demo
Details
\s
- a whitespace(?=[^"]*":\s*")
- a positive lookahead that, immediately to the right of the current position, requires[^"]*
- zero or more chars other than"
":
-":
string\s*
- 0+ whitespaces"
- a double quote.
Remove spaces in Json keys
You can use JSON.stringify()
, JSON.parse()
, String.prototype.replace()
with RegExp
/\s(?=\w+":)/g
to match space character followed by one or more word characters followed by "
followed by :
var arr = [{ "Id": "ALFKI", "Contact Name": "Maria Anders", "Contact Title": "Sales Representative", "City": "Berlin", "Slider": 10}, { "Id": "ANATR", "Contact Name": "Ana Trujillo", "Contact Title": "Owner", "City": "México D.F.", "Slider": 5}];
arr = JSON.parse(JSON.stringify(arr).replace(/\s(?=\w+":)/g, ""));
console.log(arr);
Remove leading and trailing whitespaces in JSON keys
In addition to @BillKarwin's suggestion to trim whitespace before you enter it into the database, you can also update all the values in the database to remove the spurious whitespace:
UPDATE product_json_table
SET description = REGEXP_REPLACE(description, '\\s|\\r|\\n','');
Then your original query will work:
SELECT id, product, description
FROM product_json_table
WHERE JSON_EXTRACT(description, '$.wheels') > 2;
Output:
id product description
1 truck_space {"wheels":4,"seats":3,"fuel":"diesel","mileage":8}
Demo on dbfiddle
Update
You can also perform the whitespace replacement on the fly although this will be much less efficient than permanently removing it with the UPDATE
query above:
SELECT id, product, REGEXP_REPLACE(description, '\\s|\\r|\\n','') AS description
FROM product_json_table
WHERE JSON_EXTRACT(REGEXP_REPLACE(description, '\\s|\\r|\\n',''), '$.wheels') > 2
Output:
id product description
1 truck_space {"wheels":4,"seats":3,"fuel":"diesel","mileage":8}
Demo on dbfiddle
Python - remove spaces between key and values from dictionary object
All you need to care about is the length of a string, so use len()
to compare results.
If you care about generated JSON sizes, you can trivially tell json.dumps()
to not use any spaces:
string_data = json.dumps(data, separators=(',', ':'))
Demo:
>>> import json
>>> sample = '''{"metadata":{"info":"important info"},"timestamp":"2018-04-06T12:19:38.611Z","content":{"id":"1","name":"name test","objects":[{"id":"1","url":"http://example.com","properties":[{"id":"1","value":"1"}]}]}}'''
>>> data = json.loads(sample)
>>> len(sample)
205
>>> len(json.dumps(data))
224
>>> len(json.dumps(data, separators=(',', ':')))
205
With separators
specified, the output length matches the original input. The separators
option is documented as:
If specified, separators should be an
(item_separator, key_separator)
tuple. The default is(', ', ': ')
if indent isNone
and(',', ': ')
otherwise. To get the most compact JSON representation, you should specify(',', ':')
to eliminate whitespace.
(Bold emphasis mine).
sys.getsizeof()
is the wrong tool to make comparisons here. The function gives you the memory footprint of an object (without recursing), and this memory footprint is a implementation detail of your current Python implementation and operating system specific data type sizes.
The dict
object uses 240 bytes on your OS, but this is a over-allocated hash table of references. On a different OS with different size pointers, the memory size would be different, and either way, you didn't include the size of the referenced string objects. For string objects, the memory footprint depends heavily on the highest Unicode codepoint; the string 'a'
has a different footprint from ''
, even both have length 1, because the latter contains a non-BMP codepoint so 4 bytes are needed to store just that one character, plus Python object overhead.
Next, all you saved was 19 spaces. Compression would have saved you much more, and spaces don't matter much when compressing:
>>> import zlib
>>> len(zlib.compress(json.dumps(data).encode('utf8'), 9))
155
>>> len(zlib.compress(json.dumps(data, separators=(',', ':')).encode('utf8'), 9))
154
Compression saved 50 bytes there, while using the compact JSON separators saved another single byte.
Related Topics
How to Download Fetch Response in React as File
Break the Loop of an Array Looping Function (Map, Foreach, etc.)
Postman: How to Check Whether the Field Is Returning Null in the Postman Automation
How to Open a File/Browse Dialog Using JavaScript
How to Set Image to Fit Width of the Page Using Jspdf
Ag-Grid Cellrender With Button Click
How to Add a Dynamic Action That Will Redirect to Another Page by Executing JavaScript Code
Convert 12-Hour Hh:Mm Am/Pm to 24-Hour Hh:Mm
Open the File Upload Dialogue Box Onclick the Image
How to Clear the Focus in JavaScript
How to Remove Forward and Backward Slashes from String in JavaScript
How to Reference a JavaScript Object Property With a Hyphen in It
Show Virtual Keyboard on Mobile Phones in JavaScript
Opening Pdf String in New Window With JavaScript
How to Disable Dates Before Today in Jquery Datepicker