How do I preserve undefined values when doing JSON.stringify(hash)?
Here's an example:
var hash = {
"name" : "boda",
"email" : undefined,
"country" : "africa"
};
var string = JSON.stringify(hash);
// > '{"name":"boda","country":"africa"}'
Email disappeared from JSON.stringify.
The JSON spec does not allow undefined values, but does allow null values.
You can pass a replacer function to JSON.stringify to automatically convert undefined values to null values, like this:
var string = JSON.stringify(
obj,
function(k, v) { return v === undefined ? null : v; }
);
This works for undefined values inside arrays as well, as JSON.stringify already converts those to null.
You can preserve the key by converting to null since a valid JSON does not allow undefined;
Simple one liner:
JSON.stringify(obj, (k, v) => v === undefined ? null : v)
This should do the trick
// Since 'JSON.stringify' hides 'undefined', the code bellow is necessary in
// order to display the real param that have invoked the error.
JSON.stringify(hash, (k, v) => (v === undefined) ? '__undefined' : v)
.replace(/"__undefined"/g, 'undefined')
Use null instead of undefined.
var hash = {
"name" : "boda",
"email" : null,
"country" : "africa"
};
var string = JSON.stringify(hash);
> "{"name":"boda","email":null,"country":"africa"}"
Im reading between the lines here and guessing that you want to have the value undefined when you use JSON.parse?
If that is the case you could use the following:
var encodeUndefined = function(obj, undefinedPaths, path) {
path = path || 'ROOT';
for (var key in obj) {
var keyPath = path + '.' + key;
var value = obj[key];
if (value === undefined) {
undefinedPaths.push(keyPath);
} else if (typeof value == "object" && value !== null) {
encodeUndefined(obj[key], undefinedPaths, keyPath);
}
}
}
var stringifyAndPreserveUndefined = function(obj) {
var undefinedPaths = [];
//save all paths that have are undefined in a array.
encodeUndefined((obj), undefinedPaths);
return JSON.stringify({
ROOT: obj,
undefinedPaths: undefinedPaths
}, function(k, v) { if (v === undefined) { return null; } return v; });
}
var parseAndRestoreUndefined = function(value) {
var data = JSON.parse(value);
var undefinedPaths = data.undefinedPaths;
var obj = data.ROOT;
//Restore all undefined values
for (var pathIndex = 0; pathIndex < undefinedPaths.length; pathIndex++) {
var pathParts = undefinedPaths[pathIndex].substring(5).split('.');
var item = obj;
for (var pathPartIndex = 0; pathPartIndex < pathParts.length - 1; pathPartIndex++) {
item = item[pathParts[pathPartIndex]];
}
item[pathParts[pathParts.length - 1]] = undefined;
}
return obj;
}
var input = {
test1: 'a',
test2: 'b',
test3: undefined,
test4: {
test1: 'a',
test2: undefined
}
};
var result = stringifyAndPreserveUndefined(input);
var result2 = parseAndRestoreUndefined(result);
stringifyAndPreserveUndefined will encode all undefined values in a array and when you call parseAndRestoreUndefined it will put them in the correct place again.
The one downside is the json will not look exactly like the object. In the example above it will turn into {"ROOT":{"test1":"a","test2":"b","test4":{"test1":"a"}},"undefinedPaths":["ROOT.test3","ROOT.test4.test2"]}
function stringifyWithUndefined(value: any, space: number): string {
const str = JSON.stringify(
value,
(_k, v) => v === undefined ? '__UNDEFINED__' : v,
space
);
return str.replaceAll('"__UNDEFINED__"', 'undefined');
}
Example 1:
const object = {
name: 'boda',
email: undefined,
country: 'africa'
};
console.log(stringifyWithUndefined(object, 2));
Result (string):
{
"name": "boda",
"email": undefined,
"country": "africa"
}
Example 2:
const array = [object, { object }, [[object]]];
console.log(stringifyWithUndefined(array, 2));
Result (string):
[
{
"name": "boda",
"email": undefined,
"country": "africa"
},
{
"object": {
"name": "boda",
"email": undefined,
"country": "africa"
}
},
[
[
{
"name": "boda",
"email": undefined,
"country": "africa"
}
]
]
]
Note that undefined is not valid JSON and JSON.parse() will fail with SyntaxError: Unexpected token [...] is not valid JSON if you give it the result of stringifyWithUndefined()
JSON does not have an undefined value, but we could write a workaround:
Preserving nested undefined values
I wrote 2 functions that internally uses JSON.stringify and JSON.parse and preserves nested undefined values using a value placeholder:
Equivalent to JSON.stringify:
/**
* Serialize a POJO while preserving nested `undefined` values.
*/
function serializePOJO(value, undefinedPlaceholder = "[undefined]") {
const replacer = (key, value) => (value === undefined ? undefinedPlaceholder : value);
return JSON.stringify(value, replacer);
}
Equivalent to JSON.parse:
/**
* Deserialize a POJO while preserving nested `undefined` values.
*/
function deserializePOJO(value, undefinedPlaceholder = "[undefined]") {
const pojo = JSON.parse(value);
if (pojo === undefinedPlaceholder) {
return undefined;
}
// Function that walks through nested values
function deepIterate(value, callback, parent, key) {
if (typeof value === "object" && value !== null) {
Object.entries(value).forEach(([entryKey, entryValue]) => deepIterate(entryValue, callback, value, entryKey));
} else if (Array.isArray(value)) {
value.forEach((itemValue, itemIndex) => deepIterate(itemValue, callback, value, itemIndex));
} else if (parent !== undefined) {
callback(value, parent, key);
}
}
// Replaces `undefined` placeholders
deepIterate(pojo, (value, parent, key) => {
if (value === undefinedPlaceholder) {
parent[key] = undefined;
}
});
return pojo;
}
Usage:
const source = {
foo : undefined,
bar : {
baz : undefined
}
};
const serialized = serializePOJO(source);
console.log("Serialized", serialized);
// '{"foo":"[undefined]","bar":{"baz":"[undefined]","qux":[1,"[undefined]",2]}}'
const deserialized = deserializePOJO(serialized);
console.log("Deserialized", deserialized);
Works with both object entries and array items.
The downside is that you have to choose an appropriate placeholder that will not be mistaken via a "real" source value. The placeholder is customizable via the optional undefinedPlaceholder argument.
This is specially useful to store POJO in browser local storage ;)
See also:
Gist: https://gist.github.com/yvele/f115f7dd0ed849f918f38b134ec3598a
JSFiddle: https://jsfiddle.net/n5jt2sf9/
This will cause it to print as undefined but this is INVALID json, but is valid JavaScript.
var string = JSON.stringify(obj, function(k,v){return v===undefined?"::undefined::":v}, 2).replace(new RegExp("\"::undefined::\"", 'g'), "undefined");
Related
I have a javascript object
var obj = {a:{b:'value'}};
where key 'a' is dynamic, key 'b' is constant, so I am not able to get value from obj['a'].
Is there any way to get the value of key 'b' without knowing key 'a'.
You can find all the keys of object using Object.keys(<obj>)
In your case:
key = Object.keys(obj)[0]; // will return "a"
Use this:
var obj = {a:{b:'value'}};
obj[Object.keys(obj)[0]].b
You could use Object.values, like so:
const obj = { a: { b:'value' } };
Object.values(obj)[0].b // 'value'
Try this,
res = { data: { list: { names: { blk: { cnt: 10 } } } }, test:'test' };
let val = getObjectVal(res, 'cnt')
getObjectVal(data, findKey){
let result = '';
for (let key in data) {
if (key == findKey)
result = data[findKey];
if ((typeof data[key] === "object") && (data[key] !== null)) {
if (key != findKey)
result = getObjectVal(data[key], findKey)
}
}
return result ? result : '';}
To get the value of b
var obj = {a:{b:'value'}};
console.log(obj[Object.keys(obj)[0]].b)
var obj = {a:{b:'value'}};
// Looking for each object variables in obj
Object.keys(obj).forEach(function(key){
// Looking for each object variables in the obj[key]
Object.keys(obj[key]).forEach(function(key2){
// do what you want if key_2 is b
if(key2=='b')
console.log(obj[key][key2])
})
})
I have an object whose keys I don't know but structure is basically the same. Value can be a string or another object of strings/objects. Here is an example:
d = {
"name": "Sam",
"grade": 9,
"classes": {
"a": 1,
"b": 2
},
"age": null
}
What I want now is if value is not another object, get the key name and its value. If value is null, return empty string. From the above the expected output is:
name=Sam, grade=9, a=1, b=2, age=''
Here since classes is object, it has to be looped again to get keys (a,b) and values (1,2).
I tried the following but it gives if any of the values is null, it returns an error:
Cannot convert undefined or null to object
It works well if there is no null value:
function getKeyValues(data) {
var q = '';
f(data);
function f(s) {
Object.keys(s).forEach(function(key) {
if (typeof s[key] === 'object') {
f(s[key]);
} else {
q = q + key + '=' + (s[key] == null) ? "" : s[key] + '&';
}
});
}
return q;
}
d = {
"name": "Sam",
"grade": 9,
"classes": {
"a": 1,
"b": 2
},
"age": null
}
console.log(getKeyValues(d));
Try this one:
function getKeyValues(data) {
var q = [];
var keys = Object.keys(data);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = data[key];
if (value == null) {
q.push(key + "=''");
} else if (typeof value == "object") {
q.push(getKeyValues(value));
} else {
q.push(key + "=" + value);
}
}
return q.join(",");
}
Another approach is to use the reduce method. Personally I find it a little bit cleaner.
function getKeyValues(d) {
return Object.keys(d).reduce((memo, key) => {
if (!d[key]) {
memo[key] = '';
} else if (typeof d[key] === 'object') {
Object.keys(d[key]).forEach((subKey) => {
memo[subKey] = d[key][subKey];
})
} else {
memo[key] = d[key];
}
return memo;
}, {})
}
Also, while your question is very clear, I must say that it also makes me a little bit wary. You could find yourself in some difficult debugging situations if property names are ever repeated in nested objects. For example, if
d={"name":"Sam","grade":9,"buddy":{"name":"Jeff","age":12}}
would you expect name be "Sam" or "Jeff"? A function that answers your question could return either, so that is something to be aware of going forward.
I've been trying to use
const fs = require("fs");
const settings = require("./serversettings.json")
let reason = args.join(' ');
function replacer(key, value) {
return reason;
}
fs.writeFileSync(settings, JSON.stringify(settings.logchannel, replacer))
It seems like to me that it doesn't work, so I'm trying to figure out how replacers work as MDN made me even more confused.
The replacer function takes a key and a value (as it passes through the object and its sub objects) and is expected to return a new value (of type string) that will replace the original value. If undefined is returned then the whole key-value pair is omitted in the resulting string.
Examples:
Log all key-value pairs passed to the replacer function:
var obj = {
"a": "textA",
"sub": {
"b": "textB"
}
};
var logNum = 1;
function replacer(key, value) {
console.log("--------------------------");
console.log("Log number: #" + logNum++);
console.log("Key: " + key);
console.log("Value:", value);
return value; // return the value as it is so we won't interupt JSON.stringify
}
JSON.stringify(obj, replacer);
Add the string " - altered" to all string values:
var obj = {
"a": "textA",
"sub": {
"b": "textB"
}
};
function replacer(key, value) {
if(typeof value === "string") // if the value of type string
return value + " - altered"; // then append " - altered" to it
return value; // otherwise leave it as it is
}
console.log(JSON.stringify(obj, replacer, 4));
Omitt all values of type number:
var obj = {
"a": "textA",
"age": 15,
"sub": {
"b": "textB",
"age": 25
}
};
function replacer(key, value) {
if(typeof value === "number") // if the type of this value is number
return undefined; // then return undefined so JSON.stringify will omitt it
return value; // otherwise return the value as it is
}
console.log(JSON.stringify(obj, replacer, 4));
settings is an object, not a filename.
I'm trying to replace the string in the settings named as "logchannel" to whatever I tell it to change it to
const fs = require("fs");
var settings = require("./serversettings.json");
settings.logchannel = "foo"; //specify value of logchannel here
fs.writeFileSync("./serversettings.json", JSON.stringify(settings));
{"empid":{"string":"31564604"},"joindate":{"date":2017-01-01}}
Convert the above json into below format using Java/Javascript. juzt need remove the datatype.
{"empid":"31564604","joindate":2017-01-01}
Another solution, using Array#reduce.
var obj = { empid: { string: 31564604 }, joindate: { date: '2017-01-01' }, nested: { nextLevel: { boolean: 1 } } },
newObj = Object.keys(obj).reduce(function(s, a) {
s[a] = obj[a][Object.keys(obj[a])]
return s;
}, {});
console.log(newObj);
You could use a recursive approach with an object for getting the types right.
function convert(object) {
var dataTypes = { boolean: true, string: true, date: true };
Object.keys(object).forEach(function (k) {
var key;
if (object[k] && typeof object[k] === 'object') {
key = Object.keys(object[k])[0];
if (key in dataTypes) {
object[k] = object[k][key];
} else {
convert(object[k]);
}
}
});
}
var object = { empid: { string: '31564604' }, joindate: { date: '2017-01-01' }, nested: { nextLevel: { boolean: true } } };
convert(object);
console.log(object);
If datatype is only specified for innermost element and not for the array or object then do it like.
var json = '{"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"},"a":[{"number":1}],"level1":{"level2":{"string":"abc"}}}';
// parse the string
var object = JSON.parse(json);
updateObj(object);
function updateObj(obj) {
// get all keys and iterate over them
Object.keys(obj).forEach(function(k) {
// get the nested object property
var key = Object.keys(obj[k])[0];
// update only if nested property is object
typeof obj[k][key] != 'object' && (obj[k] = obj[k][key]);
// recursively call the faction if the element is object
typeof obj[k] == 'object' && updateObj(obj[k]);
})
}
// convert back to JSON
console.log(JSON.stringify(object));
With Javascript, convert it to an object then update by simply iterating over all properties and finally stringify the object.
var json = '{"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"}}';
// parse the string
var obj = JSON.parse(json);
// get all keys and iterate over them
Object.keys(obj).forEach(function(k) {
// update the property with the nested object property value
obj[k] = obj[k][Object.keys(obj[k])[0]];
})
// convert back to JSON
console.log(JSON.stringify(obj));
UPDATE : For nested object use the same with recursive approach.
var json = '{"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"},"a":{"array":[{"number":1}]},"level1":{"object":{"level2":{"string":"abc"}}}}';
// parse the string
var object = JSON.parse(json);
updateObj(object);
function updateObj(obj) {
// get all keys and iterate over them
Object.keys(obj).forEach(function(k) {
// update the property with the nested object property value
obj[k] = obj[k][Object.keys(obj[k])[0]];
// recursively call the faction if the element is object
typeof obj[k] == 'object' && updateObj(obj[k]);
})
}
// convert back to JSON
console.log(JSON.stringify(object));
Given the data:
const oldJson = {"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"}};
ES6:
let newJson = {};
Object.keys(oldJson).forEach(key => {
newJson[key] = oldJson[key];
});
ES5:
var newJson = {};
Object.keys(oldJson).forEach(function(key) {
newJson[key] = oldJson[key];
});
My solution : only with ES6 feature so you have to use Babel
(function(){
const oldJson = {"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"}};
const newJson = Object.values( oldJson ).reduce( (acc, value) => Object.assign(acc, value), {})
console.log(newJson);
})();
Documentation :
Object.values
Object.assign
Array.reduce
Is there a quick way to get the value of a key starting with a certain string?
Example :
var obj = {
"key123" : 1,
"anotherkey" : 2
}
obj['key1'] // would return 1
obj['ano'] // would return 2
Thanks
You can create a helper function
function findValueByPrefix(object, prefix) {
for (var property in object) {
if (object.hasOwnProperty(property) &&
property.toString().startsWith(prefix)) {
return object[property];
}
}
}
findValueByPrefix(obj, "key1");
As Kenney commented, the above function will return first match.
You could use find on the entries of the object. IF there's a key which starts with, access the index at 1 to get the value.
Object.entries(o).find(([k,v]) => k.startsWith(part))?.[1]
Here's a snippet:
const getValue = (part, o) => Object.entries(o).find(([k, v]) => k.startsWith(part))?.[1]
const obj = {
"key123": 1,
"anotherkey": 2
}
console.log(
getValue('key', obj),
getValue('ano', obj),
getValue('something', obj),
)
Search for the first match of the property name which begins with specified string:
var obj = {
"key123": 1,
"anotherkey": 2,
"somekey" : 3
};
function getObjectValueByPartialName(obj, name){
if (typeof obj !== "object") {
throw new Error("object is required!");
}
for (var prop in obj) {
if (prop.indexOf(name) === 0){
return obj[prop];
}
}
return "no such property!";
}
console.log(getObjectValueByPartialName(obj, 'some')); // output: 3
console.log(getObjectValueByPartialName(obj, 'key1')); // output: 1
We could use the following one-line generic custom method
var obj = {
"key123" : 1,
"anotherkey" : 2
};
const getObjPropForMatchPrefixKey = (object,prefix) => Object.keys(object).filter(item => item.toString().startsWith(prefix))[0];
console.log(obj[getObjPropForMatchPrefixKey(obj,'an')]);
console.log(obj[getObjPropForMatchPrefixKey(obj,'key')]);