baig772
baig772

Reputation: 3498

Javascript- How to loop through invalid JSON

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?

Upvotes: 1

Views: 98

Answers (2)

Shanimal
Shanimal

Reputation: 11718

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>

Upvotes: 1

Luc
Luc

Reputation: 534

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

Upvotes: 2

Related Questions