0

I'd like to be able to selectively remove elements from a json schema. Imagine a json object that contains a larger but similar array of users like this

[{
    "users": [{
        "firstName": "Nancy",
        "socialSecurityNumber": "123-45-6789",
        "sex": "Female",
        "id": "1234",
        "race": "Smith",
        "lastName": "Logan"
    }, {
        "firstName": "Charles",
        "socialSecurityNumber": "321-54-9876",
        "sex": "Male",
        "id": "3456",
        "race": "White",
        "lastName": "Clifford"
    }],

I'd like to strip the socialSecurityNumber element from the json schema using a regex expression. What would a regex expression to remove

"socialSecurityNumber": "whatever value",

look like where the value of the data pair could be any string?

I cannot be certain of the position of the data pair and whether it would have a trailing comma.

7
  • 3
    You should not be using regular expressions for this, but rather a JSON parser. Commented Jul 21, 2016 at 2:14
  • Are you suggesting that I deserialize the json object and then run through the structure to remove the unwanted data? Commented Jul 21, 2016 at 2:28
  • 4
    Absolutely. Anything else will have weird failure conditions. Commented Jul 21, 2016 at 2:29
  • Yes, I sugest a JSON parser too, UNLESS you can guarantee that the parts you want to remove are ALWAYS in the same line. Where did this JSON come from? Commented Jul 21, 2016 at 2:49
  • It's coming from a Coldfusion/Hibernate ORM entity. I can't guarantee that the parts will always be in the same line. I'm not familiar with JSON parsers. Can you name some common parsers? The JSON will be returned as a RESTful service response and so the processing needs to be done in Coldfusion before it leaves the server. Commented Jul 21, 2016 at 3:21

1 Answer 1

1

Try replacing the following regular expression with empty:

"socialSecurityNumber": "(\d|\-)",

It can go wrong if this info is split in 2 lines, or if the SSN is the last user field, because there will be no comma after it.

Anyway, after the replacing operation, check if there are any string

"socialSecurityNumber"

to confirm this can be used. If there are still strings that weren't replaced, then you will need a JSON parser to correctly eliminate this information.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.