ljson

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 5 Imported by: 2

README

LJSON

ljson Go is a library that parse loose json string


Go Reference Go goreleaser GitHub go.mod Go version of a Go module GoReportCard GitHub license GitHub release

Install

Install the package into your code with:

go get "github.com/bububa/ljson"

Import in your code:

import (
	"github.com/bububa/ljson"
)

Example

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/bububa/ljson"
)

func main() {
	// Define the expected schema as a struct
	type Nested struct {
		A int `json:"a"`
	}

	type MySchema struct {
		Field1    []map[string]string `json:"field1"`
		NestedObj Nested              `json:"nested"`
		Numbers   int                 `json:"numbers"`
		BoolVal   bool                `json:"bool_val"`
	}

	// JSON with objects stored as strings and type mismatches
	jsonStr := `{
		"field1": "[{\"sub1\": \"xxx\"}, {\"sub2\": \"yyy\"}]",
    "nested": "{\"a\": \"123\"}",
		"numbers": "456",
		"bool_val": "true"
	}`
	arrStr := `[{
		"field1": "[{\"sub1\": \"xxx\"}, {\"sub2\": \"yyy\"}]",
    "nested": "{\"a\": \"123\"}",
		"numbers": "456",
		"bool_val": "true"
  }, {
		"field1": "[{\"sub1\": \"xxx\"}, {\"sub2\": \"yyy\"}]",
    "nested": "{\"a\": \"123\"}",
		"numbers": "456",
		"bool_val": "true"
  }]`
	mapStr := `"{\"sub1\": \"xxx\", \"sub2\": 123}"`

	// Define a struct instance to receive the parsed data
	var result MySchema

	// Unmarshal using our loose parser
	if err := ljson.Unmarshal([]byte(jsonStr), &result); err != nil {
		return
	}

	// Print the processed result
	resultJSON, _ := json.MarshalIndent(result, "", "  ")
	fmt.Println(string(resultJSON))

	// Define a struct instance to receive the parsed data
	var arrResult []MySchema

	// Unmarshal using our loose parser
	if err := l.Unmarshal([]byte(arrStr), &arrResult); err != nil {
		return
	}

	// Print the processed result
	resultJSON, _ = json.MarshalIndent(arrResult, "", "  ")
	fmt.Println(string(resultJSON))

	// Define a struct instance to receive the parsed data
	mapResult := make(map[string]string)

	// Unmarshal using our loose parser
	if err := l.Unmarshal([]byte(mapStr), &mapResult); err != nil {
		return
	}

	// Print the processed result
	resultJSON, _ = json.MarshalIndent(mapResult, "", "  ")
	fmt.Println(string(resultJSON))

	// Example with interface type
	interfaceData := `{
		"some_field": "{\"a\": \"456\"}"
	}`
	var myInterface interface{}
	if err := Unmarshal([]byte(interfaceData), &myInterface); err != nil {
		fmt.Println("Error unmarshalling interface:", err)
	} else {
		fmt.Printf("Unmarshalled interface: %+v\n", myInterface)
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal(data []byte, schema any) error

Unmarshal function that processes JSON loosely based on a schema

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL