O
O
Oleg Shevelev2016-04-30 12:38:01
JavaScript
Oleg Shevelev, 2016-04-30 12:38:01

How to parse JavaScript Object (not valid JSON) in Golang?

Valid JSON first:

v1 := `{
        "middle" : {
        "src": "pictures/product/middle/14906_middle.jpg",
        "place" : "#preview-img",
        "title": "343880-090-slantsy-nike-benassi-just-do-it"
        }
    }`

Now a valid JavaScript Object, but not valid JSON:
v2 := `{
        middle : {
        src: 'pictures/product/middle/14906_middle.jpg',
        place : '#preview-img',
        title: '343880-090-slantsy-nike-benassi-just-do-it'
        }
    }`

Tell me how to parse the second option? The usual encoding.json.Unmarshal () swears for invalidity.
error: invalid character 'm' looking for beginning of object key string

Maybe there is some library in which this nuance can be configured?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Semenko, 2016-04-30
@abler98

In js, you can convert an object to a valid json string
PS I don't understand why you can't pass a string instead of an object.

O
Oleg Shevelev, 2016-04-30
@mantyr

I have not yet compared these two options in terms of performance, but in future tasks I will prefer to use the first option.
On stackoverflow, it was advised to look at the https://godoc.org/launchpad.net/rjson#Unmarshal library , but it did not have support for values ​​in single quotes.
As a result, I forked and added support for single quotes.

package main

import (
    "fmt"
    "github.com/mantyr/rjson"
)

func main() {
    data := []byte(`{
        middle : {
            src: "pictures/product/123.jpg",
            place : '#preview-img',                // Added support for JavaScript object parsing
            title: "title"
        }
    }`)

    var v struct {
        Middle struct {
            Src string
            Place string
            Title string
        }
    }
    err := rjson.Unmarshal(data, &v)
    fmt.Println(v)   // print {{pictures/product/123.jpg #preview-img title}}
    fmt.Println(err) // print <nil>
}

https://github.com/mantyr/rjson
The original package from the author is here: https://github.com/rogpeppe/rjson
If you have any suggestions for adding rjson - write to me or offer a pool request.
You can run the VM https://github.com/robertkrimen/otto and pass it a little padded text to get the desired field from it.
package main

import (
    "testing"
    "github.com/robertkrimen/otto"
)

func BenchmarkVMGet(b *testing.B) {
    vm := otto.New()

    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        st := `
            {
                    middle : {
                    src: 'pictures/product/middle/14906_middle.jpg',
                    place : '#preview-img',
                    title: '343880-090-slantsy-nike-benassi-just-do-it'
                }
            }
        `

        vm.Run(`
            obj = `+st+`
            src = obj.middle.src;
        `)
        src, err := vm.Get("src")    // <-----
        _ = src
        _ = err
    }
}

# go test -bench=".*" ottotest
testing: warning: no tests to run
PASS
BenchmarkVMGet	   30000	     58103 ns/op
ok  	ottotest	2.367s

However, there are downsides:
Pros:
I am also looking for other options, purely for the task of parsing an object and not performing unnecessary operations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question