Table of Contents
Question content
Workaround
Home Backend Development Golang How to remove unwanted characters between keys and values ​​in JSON?

How to remove unwanted characters between keys and values ​​in JSON?

Feb 09, 2024 pm 05:15 PM
json processing

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:

  1. Delete\n\n
  2. 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())
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do PHP and MySQL handle special characters and escape symbols in JSON? How do PHP and MySQL handle special characters and escape symbols in JSON? Jul 13, 2023 pm 10:29 PM

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? How to process JSON array using PHP and MySQL? Jul 14, 2023 pm 07:13 PM

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

How to remove unwanted characters between keys and values ​​in JSON? How to remove unwanted characters between keys and values ​​in JSON? Feb 09, 2024 pm 05:15 PM

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

Conquer the pinnacle of Java JSON processing: parse and create complex data Conquer the pinnacle of Java JSON processing: parse and create complex data Mar 09, 2024 am 09:13 AM

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? How to handle numbers and floats in JSON using PHP and MySQL? Jul 13, 2023 pm 01:06 PM

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

How to process XML and JSON format data in PHP API development How to process XML and JSON format data in PHP API development Jun 17, 2023 pm 06:29 PM

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 techniques in Java development In-depth understanding of JSON processing techniques in Java development Nov 20, 2023 pm 01:17 PM

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? How to handle complex JSON data structures using PHP and MySQL? Jul 12, 2023 pm 02:55 PM

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.

See all articles