1

I have a JSON array like this

       {  
   "2070":{  
      "address_id":"2070",
      "firstname":"Simon",
      "lastname":"Hall",
      "company":"",
      "address_1":"",
      "address_2":"",
      "postcode":"44000",
      "city":"",
      "zone_id":"0",
      "zone":"",
      "zone_code":"",
      "country_id":"223",
      "country":"United States",
      "iso_code_2":"US",
      "iso_code_3":"USA",
      "address_format":"{firstname} {lastname} 
    {company} 
    {address_1} 
    {address_2} 
    {city}, {zone} {postcode} 
    {country}",
      "custom_field":false
   },
   "2071":{  
      "address_id":"2071",
      "firstname":"Simon",
      "lastname":"Hall",
      "company":"",
      "address_1":"TEST",
      "address_2":"",
      "postcode":"44000",
      "city":"New York",
      "zone_id":"3625",
      "zone":"Colorado",
      "zone_code":"CO",
      "country_id":"223",
      "country":"United States",
      "iso_code_2":"US",
      "iso_code_3":"USA",
      "address_format":"{firstname} {lastname} 
    {company} 
    {address_1} 
    {address_2} 
    {city}, {zone} {postcode} 
    {country}",
      "custom_field":false
   }
}

this JSON is invalid but I am getting it in this form, I cant get it to parse as it is not a valid JSON and I cannot remove special characterd from the string. How can I extract the data of specific index from this invalid JSON? any way to loop through this JSON?

baig772
  • 3,404
  • 11
  • 48
  • 93
  • 2
    You can get real JSON or write your own parser. I'd strongly lean toward the former. – ssube Mar 31 '16 at 18:55
  • If it's not valid JSON, you can only treat it as a string. You'll have to write your own parser for it, basically. – Pointy Mar 31 '16 at 18:55
  • Is it just the line-breaks that are causing you issues? Because you can kludge your example with `JSON.parse(str.replace(/\n/g, ''));` and the parser accepts it. – Emissary Mar 31 '16 at 19:02

2 Answers2

2

With the example that you have given you could simply remove all the line breaks from your string, resulting in valid JSON.

I did not test it, but it's probably something like this:

var json = "your json";
json = json.replace(/(?:\r\n|\r|\n)/g, '');

Modified from https://stackoverflow.com/a/5664521/2911452

Good luck.

Update

As Emissary mentioned, removing new lines results in data loss. Therefore you could replace the new lines with "JSON escaped new lines". See: https://stackoverflow.com/a/42073/2911452

Community
  • 1
  • 1
Luc
  • 544
  • 3
  • 9
  • @Emissary What about now? Sorry, I don't have enough reputation to post comments yet. – Luc Mar 31 '16 at 19:16
1

I would just replace the offending line endings

data = data.replace(/\{(lastname|company|address_1|address_2|postcode)\}\s?\n/ig,'{$1}\\\\n');
console.log(JSON.parse(data)); // Object {2070: Object, 2071: Object}
<!-- The following just mocks the response data provided -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text" id="data">
{  
   "2070":{  
      "address_id":"2070",
      "firstname":"Simon",
      "lastname":"Hall",
      "company":"",
      "address_1":"",
      "address_2":"",
      "postcode":"44000",
      "city":"",
      "zone_id":"0",
      "zone":"",
      "zone_code":"",
      "country_id":"223",
      "country":"United States",
      "iso_code_2":"US",
      "iso_code_3":"USA",
      "address_format":"{firstname} {lastname} 
    {company} 
    {address_1} 
    {address_2} 
    {city}, {zone} {postcode} 
    {country}",
      "custom_field":false
   },
   "2071":{  
      "address_id":"2071",
      "firstname":"Simon",
      "lastname":"Hall",
      "company":"",
      "address_1":"TEST",
      "address_2":"",
      "postcode":"44000",
      "city":"New York",
      "zone_id":"3625",
      "zone":"Colorado",
      "zone_code":"CO",
      "country_id":"223",
      "country":"United States",
      "iso_code_2":"US",
      "iso_code_3":"USA",
      "address_format":"{firstname} {lastname} 
    {company} 
    {address_1} 
    {address_2} 
    {city}, {zone} {postcode} 
    {country}",
      "custom_field":false
   }
}
  </script>
<script>var data = $("#data").html();</script>
Shanimal
  • 11,517
  • 7
  • 63
  • 76