How to Remove Special Character from Json Without Parsing

Javascript - Remove Special characters and associated strings in Json object

You can take advantage of a replacer function that can be passed to JSON.stringify. After stringification you can parse the JSON string back to an object using JSON.parse

let obj = [  { profile: "admin", availableAction: 'You have [Access: write][Option: print] only few options'},  { profile: "cust", availableAction: 'You have [Access: write][Option: print] only few options'}];


const result = JSON.parse(JSON.stringify(obj, (k, v) => (k === "availableAction") ? v.replace('[Access: write][Option: print] ', '') : v));
console.log(result)

How to remove special character or unwanted symbols from the file written JSON format in C# UNity?

� is the Unicode Replacement character, emitted when there's an attempt to read text as if they were encoded with a single-byte codepage using the wrong codepage. It's not a BOM - File.ReadAllText would recognize it and use it to load the rest of the file using the correct encoding. This means there's garbage at the start.

The problem is caused by the inappropriate use of BinaryWriter. That class is used to write fields of primitive types in a binary format to a stream. For variable length types like stings, the first byte(s) contain the field length.

This code :

using var ms=new MemoryStream();
using( BinaryWriter writer = new BinaryWriter(ms))
{
writer.Write(new String('0',3));
}

var b=ms.ToArray();

Produces

3, 48,48,48

Use StreamWriter or File.WriteAllText instead. The default encoding used is UTF8 so there's no need to specify an encoding or try to change anything :

using (FileStream fs = new FileStream( Path.Combine(Application.persistentDataPath , savedName+".json"), FileMode.Create))
using(var writer=new StreamWriter(fs))
{
writer.Write(jsons);
}

or

var path=Path.Combine(Application.persistentDataPath , savedName+".json")
using(var writer=new StreamWriter(path))
{
writer.Write(jsons);
}

how to remove special character in my json format

try this:

        String json = "\"{\"Employee\":[{\"id\":111589,\"firstName\":\"govind\",\"lastName\":\"Lname1\",\"company\":\"ABC\",\"No1\":1,\"Test\":null,\"Test1\":\"name1\"},{\"id\":12345,\"firstName\":\"name2\",\"lastName\":\"Lname2\",\"company\":\"ABC\",\"No1\":2,\"Test\":null,\"Test1\":\"name2\"},{\"id\":14567,\"firstName\":\"name3\",\"lastName\":\"Lname3\",\"company\":\"DEF\",\"No1\":3,\"Test\":null,\"Test1\":\"name3\"},{\"id\":1212,\"firstName\":\"govi\",\"lastName\":\"l1\",\"company\":\"Ac\",\"No1\":4,\"Test\":0,\"Test1\":\"name4\"},{\"id\":1212,\"firstName\":\"govi\",\"lastName\":\"l1\",\"company\":\"Ac\",\"No1\":5,\"Test\":0,\"Test1\":\"name4\"}]}\"";

if (json.charAt(0) == '"' && json.charAt(json.length() - 1) == '"') {

json = json.substring(1, json.length() - 1);

}

System.out.println("JSON is : " + json);

How to Remove Special Character in JSON code output in html using JAVASCRIPT

Can you not just use

.replace(/[!@#$^&%*()+=[\]/{}|:<>?,.\\-]/g, '')

This would replace all the invalid characters at once.

let str = '<pre> --all special character @#$%&*()\\??><{}[]</pre>'
str = str.replace(/[!@#$^&%*()+=[\]\/{}|:<>?,.\\-]/g, '')
console.log(str)

Regex replace of special characters in JSON string not working

When you use escapes like \n and \t in a string constant (like your first example), what you end up with is a string that contains the intended special characters (newline or tab).

When you JSON encode an object, however, what you end up with is a string that contains the escape sequences themselves, not the special characters. The work that JSON.stringify() does has to include making sure that a subsequent parse of the JSON will re-create the original values of all the string-valued object properties. Thus it turns the embedded special characters in the strings back into escape sequences. Your regular expression is looking for the special characters, not the escape sequences.

Parse Nested Json in Python to Remove Special Characters in Columns

A bit Q&D - you'll have to provide a complete implementation for fixkey but this should fix your problem.

import json

def fixkey(key):
# toy implementation
#print("fixing {}".format(key))
return key.replace("&", "").replace("$", "")

def normalize(data):
#print("normalizing {}".format(data))
if isinstance(data, dict):
data = {fixkey(key): normalize(value) for key, value in data.items()}
elif isinstance(data, list):
data = [normalize(item) for item in data]
return data

jsdata = """
{
"highest_table": {
"items": [{
"key": "Human 1",
"columns": {
"Na$me": "Tom",
"Description(ms/2)": "Table Number One on the Top",
"A&ge": "24",
"Ge_nder": "M"
}
},
{
"key": "Human 2",
"columns": {
"Na$me": "John",
"Description(ms/2)": "Table Number One on the Top",
"A&ge": "23",
"Ge_nder": "M"
}
}
]
}
}
"""


data = json.loads(jsdata)

data = normalize(data)

result = json.dumps(data, indent=2)
print(result)


Related Topics



Leave a reply



Submit