6

Can I escape a colon : that is inside a JSON string? Currently this object/value set

{ pn: "MT46H128M16LFCK-5 IT:A", 
  visible: false, 
  url: "/$ws$/29/1/products/ProductDetails.html?product=products/dram/MT46H128M16LFCK-5 IT" 
}

doesn't get read. I suspect it's due to the :A in the pn value. How do I escape it or otherwise grab it?

aynber
  • 22,380
  • 8
  • 50
  • 63
ANNE Stahl
  • 61
  • 1
  • 1
  • 2
  • 6
    You do not need to escape colons. It's really only quotes that ever need to be escaped in JSON. – Gabe Jun 25 '10 at 21:06
  • Yep, I read your post wrong with my initial answer (thought it was a jquery selector for some reason)... sorry. The space in the URL is a little dubious to me though. – Andir Jun 25 '10 at 21:11
  • @gabe, Also / in some cases. – Arashsoft Aug 19 '16 at 19:41
  • @Arashsoft: No, `/` has no special meaning within a JSON string. It's just `/`. PHP erroneously escapes them, but that's just a PHP issue. – T.J. Crowder May 11 '17 at 13:15
  • @T.J.Crowder, we have to do so if we want to embed our JSON in a ` – Arashsoft May 11 '17 at 13:58
  • @Arashsoft: No, you don't. **If** you're outputting JSON within the body of a ``. Not others. – T.J. Crowder May 11 '17 at 14:17
  • In addition to quotes, backslashes also need to be escaped (from experience, not the stndard) – John Deighan Jul 05 '17 at 00:53

1 Answers1

11

If that's your actual JSON, your problem is that the identifiers (pn, visible, url), need to be quoted ("pn", "visible", "url").

As the comment said, colons don't need to be escaped in JSON string literals.

John Flatness
  • 32,469
  • 5
  • 79
  • 81
  • Hmm, except the other value pairs work perfectly well. E.g. { pn: "MT45W8MW16BGX-856 AT", visible: true, url: "/$ws$/29/1/products/ProductDetails.html?product=products/dram/MT45W8MW16BGX-856 AT" } Works well and returns results. – ANNE Stahl Jun 25 '10 at 21:23
  • I'm using it in conjunction with autoSuggest/jQuery. Unfortunately all our dev sites are behind a firewall, so I cannot list actual urls, but here is the jquery call: – ANNE Stahl Jun 25 '10 at 21:24
  • jQuery("input#search").autoSuggest(webPartItems, { selectedItemProp: "pn", searchObjProps: "pn", selectedValuesProp: "url", startText:"Search by part number or keyword", retrieveLimit:10, minChars:2, emptyText:"No Results. Type MT1...", selectionLimit:1, selectionAdded: function(elem){ var pLink = jQuery('input.as-values').attr("value"); window.location=pLink.split(',')[0]; } – ANNE Stahl Jun 25 '10 at 21:25