Skip to content

Instantly share code, notes, and snippets.

@flori
Created September 20, 2013 10:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flori/6635785 to your computer and use it in GitHub Desktop.
Save flori/6635785 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
r = ''
for line in DATA
line.gsub!(/^(\s*"[^=]+":\s*")(.*?)(",\s*)$/) {
b, m, a = $~.captures
"#{b}#{m.gsub('"', '\\"')}#{a}"
}
puts line
r << line
end
p JSON.parse(r)
__END__
{
"key1": "foo"bar"baz",
"key2": "foo",
"key3": "foo"bar"",
"key4": "bar"
}
@xxxazxxx
Copy link

thanks @flori

@xxxazxxx
Copy link

gsub is unable escape quotes when the JSON data is in minified state.
{"key1": "foo"bar"baz","key2": "foo","key3": "foo"bar"","key4": "bar"}

I wish it worked with minified JSON !

PS : Those lists i need to process are in minified state

@xxxazxxx
Copy link

Hi @flori, To make things more complex. This is the list I'm trying to parse.

Pretty JSON

{
    "results": [
        {
            "id": "1",
            "name": "f"o"o",
            "distance": "",
            "address": "b"a"r",
            "AlsoListedIn": {
                "arr_len": "5",
                "also_content": [
                    {
                        "cat_id": "",
                        "category": "foo",
                        "category_display": "bar"
                    },
                    {
                        "cat_id": "302",
                        "category": "foo",
                        "category_display": "bar",
                        "movietime": ""
                    }
                ]
            },
            "ratings": {
                "total": "0",
                "ratings": []
            },
            "city": "xyz"
        },
        {
            "id": "2",
            "name": "f"o"o",
            "distance": "",
            "address": "b"a"r",
            "AlsoListedIn": {
                "arr_len": "5",
                "also_content": [
                    {
                        "cat_id": "",
                        "category": "foo",
                        "category_display": "bar"
                    },
                    {
                        "cat_id": "302",
                        "category": "foo",
                        "category_display": "bar",
                        "movietime": ""
                    }
                ]
            },
            "ratings": {
                "total": "0",
                "ratings": []
            },
            "city": "xyz"
        }
    ]
}

Tidy JSON

{"results":[{"id":"1","name":"f"o"o","distance":"","address":"b"a"r","AlsoListedIn":{"arr_len":"5","also_content":[{"cat_id":"","category":"foo","category_display":"bar"},{"cat_id":"302","category":"foo","category_display":"bar","movietime":""}]},"ratings":{"total":"0","ratings":[]},"city":"xyz"},{"id":"2","name":"f"o"o","distance":"","address":"b"a"r","AlsoListedIn":{"arr_len":"5","also_content":[{"cat_id":"","category":"foo","category_display":"bar"},{"cat_id":"302","category":"foo","category_display":"bar","movietime":""}]},"ratings":{"total":"0","ratings":[]},"city":"xyz"}]}

I'll be so thankful to you if you could help out !

@xxxazxxx
Copy link

@flori I'm unable to PREPROCESS the Tidy JSON. I request you to help me out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment