smartjson

package module
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: Apache-2.0 Imports: 2 Imported by: 2

README

Go Reference

SmartJSON - Go package to handle JSON

The smartjson is a go package to handle parsed JSON files more confortable. This package is not a parser. It uses the built-in "encoding/json" package as parser. It gives you some convenient helper functions to query/view/convert parsed JSON data structures.

In order to use it you have to get the package

go get github.com/hyper-prog/smartjson

It seems that "go get ..." does not always work above go version 1.16. To workaround this set the "GO111MODULE=auto" environment variable during go get.

Check out the https://github.com/hyper-prog/smartyaml package to work with YAML

Sample code:

package main

import (
	"fmt"
	"github.com/hyper-prog/smartjson"
)

var samplejson string = `
{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [],
  "spouse": null
}`

func main() {
	/* // Json load from file:
	jsonData, flerr := ioutil.ReadFile("test.json")
	if flerr != nil {
		fmt.Println("Error, cannot read file: ", flerr.Error())
	}
	sj, parsererror := smartjson.ParseJSON(jsonData)
	*/

	sj, parsererror := smartjson.ParseJSON([]byte(samplejson))
	if parsererror != nil {
		fmt.Println("Error, not valid JSON: ", parsererror.Error())
	}

	fmt.Println("City: ", sj.GetStringByPathWithDefault("$.address.city", "Unknown"))
	fmt.Println("First phone number: ", sj.GetStringByPathWithDefault("/phoneNumbers/[0]/number", "Not available"))
	_,agetype := sj.GetNodeByPath("age")
	fmt.Println("Type of Age: ", agetype)
	
	fmt.Println("Yaml:")
	fmt.Println()
	fmt.Println(sj.Yaml())
}

Query path types

You can query the nodes like directories, separated with / where the root sign is optional.

You can also use JsonPath to query a value. JsonPath mode is automatic if the query string starts with "$." or "JsonPath:" prefix

/members/[0]/name - Name of the first member
$.members[2]/name - Name of the third member
/config/items/[]/description  - The description of the last config item
$.config.server.address - The address of the configured server

Available functions

Function Description
ParseJSON(rawdata []byte) Parse the JSON data
Yaml() Generates a YAML string
JsonIndented() Generates an indented JSON string
JsonCompacted() Generates a compacted JSON string
NodeExists(path string) True if the given node exists
GetCountDescendantsByPath(path string) Gives the number of descendants of the node
GetNodeByPath(path string) Gives the node and node type by path
GetStringByPath(path string) String value of the requested node
GetNumberByPath(path string) Float or int value as float of the requested node
GetFloat64ByPath(path string) Float value of the requested node
GetIntegerByPath(path string) Integer value of the requested node
GetTimeByPath(path string) Time value of the requested node
GetBoolByPath(path string) Bool value of the requested node
GetStringByPathWithDefault(path string, def string) String value of the requested node with fallback value
GetNumberByPathWithDefault(path string, def float64) Float or int value as float of the requested node with fallback value
GetFloat64ByPathWithDefault(path string, def float64) Float value of the requested node with fallback value
GetIntegerByPathWithDefault(path string, def int) Integer value of the requested node with fallback value
GetTimeByPathWithDefault(path string, def time.Time) Time value of the requested node with fallback value
GetBoolByPathWithDefault(path string, def bool) Bool value of the requested node with fallback value
GetMapByPath(path string) The map on the path
GetArrayByPath(path string) The array on the path
GetSubjsonByPath(path string) Gives the sub json specified by path

Author, License

The package is written by Peter Deak (C) hyper80@gmail.com under Apache 2.0 license

Documentation

Overview

smartjson is a go package to handle parsed json files more confortable. It provides helper functions to parse, query and covert the json data

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SmartJSON

type SmartJSON struct {
	smartjsonyamlstub.SmartJsonYamlBase
}

SmartJSON holds the parsed data. You can call the smartjson functions on this structure

func ParseJSON

func ParseJSON(rawdata []byte) (SmartJSON, error)

ParseJSON parse the raw json data (read from file) and returns a SmartJSON structure

func (SmartJSON) GetSubjsonByPath

func (smartJson SmartJSON) GetSubjsonByPath(path string) (SmartJSON, string)

GetSubjsonByPath returns an another SmartJSON struct which holds a part of the parsed json specified by path

func (SmartJSON) String

func (smartJson SmartJSON) String() string

String generate an indented json string

Jump to

Keyboard shortcuts

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