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


First, a valid JSON:

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

Now 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 () complains about being invalid.

error: invalid character 'm' looking for beginning of object key string

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


You can start the VM and pass it a little augmented text to get the desired field from it.

package main

import (

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

    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'

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

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

However, there are downsides:

  • an arbitrary piece of text can fly in and execute it thoughtlessly incorrectly (remember eval in php )
  • slow enough
  • very large library used – bad dependency


  • still fast enough
  • you can not stop the VM, but push there through Run, but if pushing endlessly, the question remains – will it die once or not – without a good knowledge of the internals of the library, this cannot be said in advance

I'm still looking for other options, purely for the task of parsing the object and not performing unnecessary operations.


There is a library for this at . If the key matches the regex


Note that in JavaScript the $ symbol can be used – but not here.

However, does not support single quotes in values. To fix this, a fork was made which can be taken here:

Scroll to Top