


How to remove unwanted characters between keys and values in JSON?
php editor Strawberry is here to share with you a tip about JSON processing: How to delete unnecessary characters between keys and values in JSON? When processing JSON data, we often need to clean up some useless characters to improve the readability of the data and reduce the size of data transmission. By using PHP's related functions and regular expressions, we can easily remove unnecessary characters between keys and values in JSON, making the data more tidy and standardized. Next, I will introduce the specific operation steps and code implementation in detail.
Question content
I have this JSON: {"key1": "value1", \n \n "key2": "value2\nwithnewline"}
I want:
- Delete\n\n
- Retain value2\n and newline characters.
So I will have a valid JSON.
I tried :regex but couldn't figure out how to specify outside of keys and values.
and this:
package main import ( "bytes" "fmt" ) func main() { jsonStr := `{"key1": "value1", \n \n "key2": "value2\nwithnewline"}` var cleaned bytes.Buffer quoteCount := 0 for i := 0; i < len(jsonStr); i++ { if jsonStr[i] == '"' { quoteCount++ } if quoteCount%2 == 0 && jsonStr[i] != '\n' { cleaned.WriteByte(jsonStr[i]) } else if quoteCount%2 == 1 { cleaned.WriteByte(jsonStr[i]) } } fmt.Println(cleaned.String()) }
Go playground link: https://go.dev/play/p/zvNSCuE4SjQ
This doesn't work as it could \n
Actually it does \
n
Workaround
Given the parameters of your question, you can use strings.ReplaceAll
to replace all\n\n:
cleaned := strings.ReplaceAll(input, "\\n \\n ", "")
If you want to continue using the current strategy, you will encounter some problems. One of them is that regardless of your condition, you always write the string: cleaned.WriteByte(jsonStr[i])
happens in your if and else cases.
The above is the detailed content of How to remove unwanted characters between keys and values in JSON?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How do PHP and MySQL handle special characters and escape symbols in JSON? With the rapid development of the Internet, JSON (JavaScript Object Notation), as a lightweight data exchange format, is widely used in front-end and back-end data interaction. In PHP and MySQL, the ability to process JSON data is also becoming increasingly important. However, sometimes JSON data contains special characters and escape symbols, which may cause the data to be parsed and displayed incorrectly. in the text

How to process JSON array using PHP and MySQL? Introduction: In modern web application development, processing JSON data has become a common requirement. PHP and MySQL are widely used technologies, and understanding how to use them to process JSON arrays will greatly improve development efficiency and flexibility. This article will introduce how to use PHP and MySQL to process JSON arrays, and provide corresponding code examples. 1. Create a database table. Before using PHP and MySQL to process JSON arrays, we

I have this JSON: {"key1":"value1",\n\n"key2":"value2\nwithnewline"} I want: Remove\n\nKeep value2\n and newline. So I will have a valid JSON. I've tried :regex but couldn't figure out how to specify outside of keys and values. And this: packagemainimport("bytes""fmt")funcmain(){jsonStr:=`{"key1":"value1",\n\n

Parsing JSON data Parsing JSON data is a critical step in processing complex data. In Java, we can use the following methods: Use the Gson library: Gson is a widely used jsON parsing library that provides a concise and efficient api, as shown below: Gsongson=newGson();JsonObjectjsonObject=gson.fromJson(jsonString ,JsonObject.class); Using the Jackson library: Jackson is another popular JSON processing library that supports rich functionality and conversion to other formats (such as XML), as shown below: ObjectMappe

How to handle numbers and floats in JSON using PHP and MySQL? In modern web development, data transmission and storage usually use JSON format. JSON is a lightweight data exchange format that is widely used in front-end and back-end data transfer and API interfaces. When processing JSON data, you often encounter the need to process numbers and floating point numbers. This article will explain how to use PHP and MySQL to process numbers and floating point numbers in JSON. Parsing out numbers or floats from JSON in PHP is possible

In modern software development, many applications need to interact through APIs (Application Programming Interfaces), allowing data sharing and communication between different applications. In PHP development, APIs are a common technology that allow PHP developers to integrate with other systems and work with different data formats. In this article, we will explore how to handle XML and JSON format data in PHPAPI development. XML format data processing XML (Extensible Markup Language) is a commonly used data format used in various

In-depth understanding of JSON processing skills in Java development Summary: With the development of the Internet and the widespread application of data interaction, processing JSON data has become an indispensable part of modern software development. This article will delve into JSON processing techniques in Java development, including the basic concepts of JSON, the benefits of using JSON, and commonly used JSON processing tools in Java development. 1. The basic concept of JSON JSON (JavaScriptObjectNotation) is

How to handle complex JSON data structures using PHP and MySQL? In modern web development, JSON (JavaScriptObjectNotation) has become one of the main formats for exchanging data. When dealing with complex JSON data structures, PHP and MySQL provide powerful tools and functions to help us process and manipulate data efficiently. This article will introduce how to use PHP and MySQL to process and manipulate complex JSON data structures, and provide code examples.
