Comments (6)
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.
@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.
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.
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.
- You don't need to
ioutil.ReadAll(strings.NewReader
to convert a string to bytes. Just[]byte(str)
. Still allocation occurs though. strings.Join
is inefficient than usingstrings.Builder
.- 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.
Thank you so much! You're a hero! I'm going to investigate how this works.
from gojq.
Related Issues (20)
- How to pass CLI flags (null-input) when using gojq as package HOT 7
- Array slice difference to jq when using non-integer indexes HOT 8
- `def walk` should not use map_values for arrays HOT 1
- How can I slurp from code? HOT 4
- Difference between these two functions HOT 3
- path(getpath()) roundtrip doesn't work for array iterators - .[] HOT 3
- Raw output HOT 1
- gojq -nr '["\u0000"] | @tsv' HOT 6
- Sub and gsub functions HOT 2
- first/1 and --stream HOT 9
- can u provide gojq rpm package ? HOT 1
- concurrency issue detected with go1.19 HOT 2
- undocumented discrepancy re test/1 HOT 2
- gohq won't compile with the latest golang:alpine on ARM64 HOT 6
- Inability to provide user-defined indexing funtion HOT 5
- Panic in `repeatString` when repeating an empty string more than `9223372036854775807 - 512` times HOT 1
- Only getting `gojq: compile error: function not defined: keys_unsorted/0` HOT 3
- normalizeNumbers panic HOT 12
- go 1.20.1 has security issues HOT 2
- Non-integer decimal numbers lose precision HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gojq.