skip to Main Content

My json:

{ "question_1": 
  { "type"  : "string"
  , "title" : "1. 1. What did he want to make for dinner?"
  , "enum": 
    [ " a.) He wanted to make some salad and spaghetti"
    , " b.) He wanted to make some pasta salad"
    ] 
  , "required": false
  } 
, "question_2": 
  { "type": "string"
  , "title": "2. 2. Did he have the ingredients to make dinner?"
  , "enum": 
    [ " a.) Yes, he had the ingredients"
    , " b.) No, he didn't have the ingredients"
    ] 
  , "required": false
  } 
, "question_3": 
  { "type"  : "string"
  , "title" : "3. 3. Where did he go shopping?"
  , "enum": 
    [ " a.) He went to Albertsons"
    , " b.) He went to Albertos"
    ] 
  , "required": false
  } 
}

in my json there are many numbers next to each other and are duplicated

Eg:

1. 1. => 1.
2. 2. => 2.
3. 3. => 3.

and so on

How can i remove this duplication?

I want to remove duplicate numbers next to each other in json

2

Answers


  1. You can replace it using a regex. Using grouping references (the parenthesis around (d) form a group of one decimal and the 1 refers to it) will make sure that you don’t remove numbers like "1. 2." and allow you to refer to it during replacement:

    const data =
      { "question_1": 
        { "type"  : "string"
        , "title" : "1. 1. What did he want to make for dinner?"
        , "enum": 
          [ " a.) He wanted to make some salad and spaghetti"
          , " b.) He wanted to make some pasta salad"
          ] 
        , "required": false
        } 
      , "question_2": 
        { "type": "string"
        , "title": "2. 2. Did he have the ingredients to make dinner?"
        , "enum": 
          [ " a.) Yes, he had the ingredients"
          , " b.) No, he didn't have the ingredients"
          ] 
        , "required": false
        } 
      , "question_3": 
        { "type"  : "string"
        , "title" : "3. 3. Where did he go shopping?"
        , "enum": 
          [ " a.) He went to Albertsons"
          , " b.) He went to Albertos"
          ] 
        , "required": false
        } 
      };
    
    Object.values(data).forEach(v => v.title = v.title.replace(/(d). 1./, '$1.'))
    console.log(Object.values(data).map(v => v.title))
    Login or Signup to reply.
  2. If it actually is JSON, which you are going to parse using JSON.parse, you could make use of the optional reviver parameter.

    reviver

    If a function, this prescribes how each value originally produced by
    parsing is transformed before being returned. Non-callable values are
    ignored. The function is called with the following arguments:

    key

    The key associated with the value.

    value

    The value produced by parsing.

    const JSONString = `{
      "question_1": {
        "type": "string",
        "title" : "1. 1. What did he want to make for dinner?"
      },
      "question_2": {
        "type": "string",
        "title": "2. 2. Did he have the ingredients to make dinner?"
      } 
    }`;
    
    console.log(
      JSON.parse(JSONString, (key, value) => typeof value === 'string' ? value.replace(/(d). 1./, '$1.') : value)
    )

    Expanding on Moritz Ringler’s solution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search