JSON Stringify ignore key in nesten object?

json.stringify replacer
json stringify escape quotes
json stringify array of objects
json stringify c#
json.stringify pretty
json stringify returning empty object
get value from json object in javascript
stringify json online

My Routine Object looks like this:

export class Routine {
    id: string;
    name: string;
    exercises: RoutineExercise[];

My RoutineExercise Object looks like this:

export class RoutineExercise {

    id: string;
    presetKey: string;
    preset: Preset;

I need to transform a Routine Object into a JSON object but I need to exclude the preset Object from every RoutineExercise

So I need todo something like this:

function replacer(key, value) {
    if (key === "preset") {return undefined; }

const jsonString = JSON.stringify(routine, replacer);

But it doesn't exclute my presets. Probably because the presetkey is nested in the exercise key.

Any Idea how to do this?

Your replacer function is not correct. It needs to return the value whenever you want to include the key/value in the JSON. Your code makes it only return undefined whatever the key/value.

So use this one:

function replacer(key, value) {
    if (key !== "preset") { return value; }

Or alternatively, you could test for the type of the value:

function replacer(key, value) {
    if (!(value instanceof Preset)) { return value; }

JSON.stringify(), The JSON.stringify() method converts a JavaScript object or value to a All Symbol -keyed properties will be completely ignored, even when As a function, it takes two parameters: the key and the value being stringified. JSON.stringify() can return undefined when passing in "pure" values like JSON.stringify(function(){}) or JSON.stringify(undefined). All Symbol -keyed properties will be completely ignored, even when using the replacer function.

Delete the key on a copy of the objecrt before stringifying the object to get your result. You can create some sort of function that deletes a key from an object if it exists.

var obj = {
    id: '1',
    presetKey: 'abc',
    preset: 'preset',

function filterPreset(obj, key) {
  if (typeof obj === 'object' && obj[key]) {
    var copy = Object.assign({}, obj);
    delete copy[key];
    return copy;
  return obj;

console.log('result', JSON.stringify(filterPreset(obj, 'preset')));
console.log('no mutation', obj);

Using a Replacer or Filter with JSON.stringify, JSON.stringify has an optional second argument: a replacer or filter that can be a First we demonstrate applying a filter array to a JavaScript object. The output of console.log indicates that our filter has been ignored and that the entire array� In JSON, functions are not allowed as object values. The JSON.stringify() function will remove any functions from a JavaScript object, both the key and the value: Example

You can use the ... (spread operator) to deconstruct the object by key and use the rest object which doesn't contain the preset key.

const data = {
    id: '1234',
    presetKey: 'xyz',
    preset: 'customPreset'

const { preset, ...rest } = data;


JSON methods, toJSON, The method JSON.stringify(student) takes the object and converts it into a string. ignored something : undefined // ignored } ; alert ( JSON . stringify ( user ) ) The function will be called for every (key, value) pair and should� Given an object and the task is to sort the JavaScript Object on the basis of keys. Here are a few of the most used techniques discussed with the help of JavaScript. Approach 1: By using .sort() method to sort the keys according to the conditions specified in the function and get the sorted keys in the array.

The 80/20 Guide to JSON.stringify in JavaScript, JSON.stringify() throws an error when it detects a cyclical object. If spaces is not undefined, JSON.stringify() will put each key in the JSON output on If toJSON() returns undefined , JSON.stringify() will ignore that property. JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma.

Everything You Need To Know About JSON.stringify and JSON.parse, The beauty of JSON.stringify is that, in addition to objects, it also Another thing to note is that because JSON is its own language, JSON.stringify will ignore replacer() will expose the key and value to you and if you set the� The method JSON.stringify(student) takes the object and converts it into a string. The resulting json string is called a JSON-encoded or serialized or stringified or marshalled object. We are ready to send it over the wire or put into a plain data store. Please note that a JSON-encoded object has several important differences from the object

JSON.stringify(), The JSON.stringify() method converts a JavaScript value to a JSON All symbol- keyed properties will be completely ignored, even when The object in which the key was found is provided as the replacer's this parameter. You can set a descriptive key for an element in the object and then say if it's true or false. For example, tests["Body contains user_id"] = responsebody.has("user_id"); will check whether the response body contains the user_id string. You can add as many keys as needed, depending on how many things you want to test for.

  • Your replacer function should return the value when you need to keep it, so use this: if (key !== "preset") return value; The other case is undefined, but that is the default.
  • Thanks that was what I'm looking for!
  • Nope, it creates a copy.
  • Would need to be recursive for it to deal with nested objects.