Coder Social home page Coder Social logo

jsontoloyo's Introduction

JSON Mapping-Traversing library

this library can traverse and do simple mapping of 1 json (map[string]interface{}) to another json. For now it can't deals with aggregating json array into single json.

Example for simple traversing

    input := "$.name.first"
	source := strings.NewReader(input)
	parser := jsontoloyo.NewParser(source)
	selectors := []*jsontoloyo.Selector{}
	sourceMap := map[string]interface{}{}
	sourceMap["name"] = map[string]interface{}{"first": "LL", "last": "XX"}

	err := parser.Parse(&selectors)
	if err != nil {
		os.Exit(-1)
	}
	fmt.Println(selectors)
	if len(selectors) != 3 {
		os.Exit(-1)
	}
	traverser := jsontoloyo.NewTraverser(sourceMap, selectors)
	kk, err := traverser.Traverse()
	if err != nil {
		os.Exit(-1)
	}
	fmt.Println(kk) // this should output LL

Get specific index of array

    input := "$.ii[1]"
	source := strings.NewReader(input)
	parser := jsontoloyo.NewParser(source)
	selectors := []*jsontoloyo.Selector{}
	sourceMap := map[string]interface{}{}
	sourceMap["ii"] = []int{0, 1, 2, 3, 4}
	err := parser.Parse(&selectors)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(-1)
	}
	traverser := jsontoloyo.NewTraverser(sourceMap, selectors)
	kk, err := traverser.Traverse()
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(-1)
	}
    fmt.Println(kk)// kk should be 1

get aggregate of an array

    input := "$.$sum($.ii)"
	source := strings.NewReader(input)
	parser := jsontoloyo.NewParser(source)
	selectors := []*jsontoloyo.Selector{}
	sourceMap := map[string]interface{}{}
	sourceMap["ii"] = []int{0, 1, 2, 3, 4}
	err := parser.Parse(&selectors)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(-1)
	}
	if len(selectors) != 2 {
		t.Fail()
	}
	traverser := jsontoloyo.NewTraverser(sourceMap, selectors)
	kk, err := traverser.Traverse()
	if err != nil {
		t.Log(err.Error())
		t.Fail()
	}
    fmt.Println(kk)//kk should be 10

basic mapping json to json

    source := map[string]interface{}{
		"scores": []int{10, 10, 40},
	}
	sink := map[string]interface{}{}

	mappings := jsontoloyo.NewMappers()
	mappings.AddMapping("avg_score", "$.$avg($.scores)")
	err := mappings.Mapping(source, sink)
	if err != nil {
		t.Log("err", err.Error())
		t.Fail()
	}
	if _, ok := sink["avg_score"]; !ok {
		t.Log("No new field")
		t.FailNow()
	}
	if sink["avg_score"] != 20.0 {
		t.Log("avg", sink["avg_score"])
		t.Fail()
	}

Basic mapping from json array to json array

	source := []map[string]interface{}{
		map[string]interface{}{
			"scores": []int{10, 10, 40},
		},
		map[string]interface{}{
			"scores": []int{10, 20},
		},
	}
	sink := []map[string]interface{}{}
	mappings := jsontoloyo.NewMappers()
	mappings.AddMapping("avg_score", "$.$avg($.scores)")
	err := mappings.MappingArray(&source, &sink)
	if err != nil {
		t.Log("err", err.Error())
		t.Fail()
	}
	if len(sink) != 2 {
		t.FailNow()
	}
	if sink[0]["avg_score"] != 20.00 {
		t.Fail()
	}
	if sink[1]["avg_score"] != 15.00 {
		t.Fail()
	}

jsontoloyo's People

Contributors

kharism avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.