Giter Club home page Giter Club logo

Comments (6)

itchyny avatar itchyny commented on August 18, 2024

What you are doing is giving the JSON as a string input. You need to decode to an interface.

	var input interface{}
	json.Unmarshal([]byte(`[{"how":"does", "this":"work"},{"how":"does", "this":"work"}]`), &input)

from gojq.

lukepon avatar lukepon commented on August 18, 2024

@itchyny thanks, sorry it took long to reply, I overlooked that the interface{} was in fact just that (the new output with your edit in tester.json:)

map[string]interface {}{"how":"does", "this":"work"} map[string]interface {}{"how":"does", "this":"work"}

I was trying to create a newline delimited JSON from an array that I'm GETting. I think this package is beyond my abilities at the moment.

EDIT: this question can be closed

from gojq.

itchyny avatar itchyny commented on August 18, 2024

You don't need to use gojq.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	var input []interface{}
	json.Unmarshal([]byte(`[{"how":"does", "this":"work"},{"how":"does", "this":"work"}]`), &input)
	for _, x := range input {
		xs, _ := json.Marshal(x)
		fmt.Println(string(xs))
	}
}

from gojq.

lukepon avatar lukepon commented on August 18, 2024

Thank you @itchyny! That's really nice of you. I didn't think of using the interface, that helps make it more generic (which is why I was looking at gojq for an implementation of an iterator). I ended up doing this:

	var ndJSONMarshalled []byte
	var arrayOfStrings []string
	var input []interface{}
	err := json.Unmarshal([]byte(body), &input)
	if err != nil {
		..
	}
	for _, line := range input {
		s, err := json.Marshal(line)
		if err != nil {
			..
		}
		arrayOfStrings = append(arrayOfStrings, string(s))
	}
	newLineJSONString := strings.Join(arrayOfStrings, "\n")
	newLineJSONBytes, err := ioutil.ReadAll(strings.NewReader(newLineJSONString))
	if err != nil {
		..
	}
	ndJSONMarshalled = append(ndJSONMarshalled, newLineJSONBytes...)
	// ioutil.WriteFile("tester.json", ndJSONMarshalled, 0644)

It works but it feels inefficient because I'm switching from strings to []byte so many times.

from gojq.

itchyny avatar itchyny commented on August 18, 2024
  1. You don't need to ioutil.ReadAll(strings.NewReader to convert a string to bytes. Just []byte(str). Still allocation occurs though.
  2. strings.Join is inefficient than using strings.Builder.
  3. Anyway, building the JSON string (or bytes) allocates successive memory. You can write to the file using json.NewEncoder.
package main

import (
	"encoding/json"
	"log"
	"os"
)

func main() {
	body := []byte(`[{"how":"does", "this":"work"},{"how":"does", "this":"work"}]`)
	f, err := os.Create("test.json")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	var input []interface{}
	if err := json.Unmarshal(body, &input); err != nil {
		log.Fatal(err)
	}
	enc := json.NewEncoder(f)
	for _, x := range input {
		if err := enc.Encode(x); err != nil {
			log.Fatal(err)
		}
	}
}

Good luck.

from gojq.

lukepon avatar lukepon commented on August 18, 2024

Thank you so much! You're a hero! I'm going to investigate how this works.

from gojq.

Related Issues (20)

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.