Comments (7)
Seems a conflict with the newline escapes and your interpreter vs json linter. It's valid JSON if you drop the single quote wrapping and test it, but when the interpreter grabs the string it handles the \n escapes ahead of the JSON.parse call.
You fix it by escaping the backslash ahead of the newline escapes: \\n
from json-js.
I don't quite understand, you mean inside the json2.js
from json-js.
No there's nothing wrong with json2.js
I'm saying that the string you're parsing is not the string you think you're parsing, because of the newline (\n
) characters.
This fails for the same reason that your input string does:
JSON.parse('{"a": "\n"}')
from json-js.
See this: https://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json
from json-js.
Ok, by doing this it worked. I thought that it was a bug but going by that thread it's by design.
Here's the fix:
String.prototype.jsonEscape = function() {
return this.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g, "\\\\t");
}
var str = '{ "kind": "youtube#playlistItemListResponse", "etag": "p4VTdlkQv3HQeTEaXgvLePAydmU/Vc3EkgLSTjYGqvhPMXul5T13c6k", "pageInfo": { "totalResults": 19, "resultsPerPage": 50 }, "items": [ { "kind": "youtube#playlistItem", "etag": "p4VTdlkQv3HQeTEaXgvLePAydmU/7hcDWTrN7EFhot4DpXw8fi97qUo", "id": "UExvQXI3RzlNaGtuOG1jTTR2VjE4SlY1RG5EMnJLQUYxNC5CMUM0NzY5NzdEQzlGRjAx", "snippet": { "publishedAt": "2019-12-23T17:23:10.000Z", "channelId": "UCaSq01bKPGAmrOnjpzBFDPQ", "title": "Regal - Repeat", "description": "Released by: Involve Records \n\nRelease/catalogue number: INV008 \n\nRelease date: Jun 15, 2015\n\nBuy and support\nhttp://decks.de/t/regal-alma_mater_ep/c55-r6\n\nhttp://facebook.com/RegalOfficial\nhttp://soundcloud.com/regalmusic\n\nhttp://involverecords.es\nhttp://facebook.com/involverecords\nhttp://soundcloud.com/involve-records\n\nRegal is back on his own imprint with a 4 cuts powerful EP. Two acid cuts, as couldn’t be otherwise, plus two dark, but full of soul, techno tracks for the mental moment of the night.\n\nAll rights reserved for the producers of these tracks.", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/7QPrJa7z12s/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/7QPrJa7z12s/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/7QPrJa7z12s/hqdefault.jpg", "width": 480, "height": 360 }, "standard": { "url": "https://i.ytimg.com/vi/7QPrJa7z12s/sddefault.jpg", "width": 640, "height": 480 }, "maxres": { "url": "https://i.ytimg.com/vi/7QPrJa7z12s/maxresdefault.jpg", "width": 1280, "height": 720 } }, "channelTitle": "Channelxyz", "playlistId": "PLoAr7G9Mhkn8mcM4vV18JV5DnD2rKAF14", "position": 0, "resourceId": { "kind": "youtube#video", "videoId": "7QPrJa7z12s" } }, "contentDetails": { "videoId": "7QPrJa7z12s", "videoPublishedAt": "2015-07-02T15:07:27.000Z" } } ] }';
var str = str.jsonEscape();
var obj = JSON.parse(str, function (key, value) {
return value;
});
console.log(obj.etag);
// p4VTdlkQv3HQeTEaXgvLePAydmU/Vc3EkgLSTjYGqvhPMXul5T13c6k
Thanks for your help, James.
from json-js.
It is not safe to add stuff to the system's prototypes. A better solution is to use JSON.stringify.
Happy new year.
from json-js.
Thanks for the tip, Douglas. Happy new year to you also!
from json-js.
Related Issues (20)
- Request for an explicit license for the package HOT 4
- License? HOT 1
- how to use npm to install HOT 1
- what's the license ?
- Json syntax HOT 2
- Json syntax HOT 1
- BigInt support HOT 2
- Include the reference to NPM package in Readme HOT 3
- Command Injection in argument HOT 1
- Cannot parse data, error: TypeError: HOT 1
- Please add license HOT 4
- How to support Object.defineProperty HOT 1
- Mind the gap between the train and the platform... :) HOT 1
- halo
- Recursive str crash HOT 1
- Licencce HOT 1
- json parse: get wrong result HOT 2
- False positives when detecting cyclic stucture HOT 5
- False positives when detecting cyclic stucture, this time with correct source file naming 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 json-js.