Created
September 20, 2013 10:39
-
-
Save flori/6635785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
} |
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
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 !
@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
thanks @flori