github.com/viant/toolbox@v0.34.5/data/udf/load.go (about)

     1  package udf
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/pkg/errors"
     6  	"github.com/viant/toolbox"
     7  	"github.com/viant/toolbox/data"
     8  	"io/ioutil"
     9  )
    10  //LoadJSON loads new line delimited or regular JSON into data structure
    11  func LoadJSON(source interface{}, state data.Map) (interface{}, error) {
    12  	location := toolbox.AsString(source)
    13  	if location == "" {
    14  		return nil, errors.New("location was empty at LoadJSON")
    15  	}
    16  	data, err := ioutil.ReadFile(location)
    17  	if err != nil {
    18  		return nil, errors.Wrapf(err, "failed to load: %v", location)
    19  	}
    20  	JSON := string(data)
    21  	if toolbox.IsNewLineDelimitedJSON(JSON) {
    22  		slice, err :=  toolbox.NewLineDelimitedJSON(JSON)
    23  		if err != nil {
    24  			return nil, err
    25  		}
    26  		var result = make([]interface{}, 0)
    27  		toolbox.ProcessSlice(slice, func(item interface{}) bool {
    28  			if item == nil {
    29  				return true
    30  			}
    31  			if toolbox.IsMap(item) && len(toolbox.AsMap(item)) == 0 {
    32  				return true
    33  			}
    34  			result = append(result, item)
    35  			return true
    36  		})
    37  		return result, nil
    38  	}
    39  	var result interface{}
    40  	err = json.Unmarshal(data, &result)
    41  	return result, err
    42  }