0

So this is my JSON.stringify'd return before I try to run JSON.parse

{"id":"2","name":"<small>L</small>(+)-Amethopterin Hydrate","class":"6.1","subclass":"","packing_group":"III","un":"2811","cas":"133073-73-1","poisons":"","hazardous":"Yes","restricted":"No","epa":"","added_by":"0","carcinogen":null},
{"id":"3","name":"(+)-Biotin 4-Nitrophenyl ester","class":"","subclass":"","packing_group":"","un":"","cas":"33755-53-2","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null},
{"id":"4","name":"(+)-Biotin N-hydroxysuccinimide ester","class":"","subclass":"","packing_group":"","un":"","cas":"35013-72-0","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}

When I try to JSON.parse I get Unexpected end of JSON input. And I can't access it as a JSON object because it will say can't define id or something to that extent.



JSON.parse(this.searchService.searchJson(this.php_url));

this.searchService.searchJson(this.php_url) is basically what my JSON string is. Gives error as mentioned above.



Also if I just try to stringify 1 of the 3 elements, it'll give me Unexpected token u in JSON at position 0



Calling function:

searchJson(url: any): any 
{   
    let items: any = []; 
    let new_data: any = []; 
    $.getJSON(url , 
        function(data)
        {   
            let temp_items: any = {}; 
            console.log(data);
            $.each(data, function (key, val)
            {   
                new_data.push(JSON.stringify(val));
            }); 
        }); 
    return new_data;
} 
5
  • 1
    Mind showing us the parse code? Commented Dec 9, 2016 at 4:12
  • @TheLazySloth Updated it. What's returned is basically an array of stringified stuff. Commented Dec 9, 2016 at 4:14
  • 1
    That is 3 objects written in commas.. It should be an array or something.. jsfiddle.net/c8743h70 Commented Dec 9, 2016 at 4:14
  • 2
    Entered the JSON at JSONLint.com and it showed an error. Added it in a [ ] and the errors disappeared. Give that a shot? Commented Dec 9, 2016 at 4:19
  • @echonax The thing is... I'm pretty sure they're in an array, I'll update with my calling function Commented Dec 9, 2016 at 4:19

3 Answers 3

2

You have to wrap it with [] because that is an array of objects:

const data = [{"id":"2","name":"<small>L</small>(+)-Amethopterin Hydrate","class":"6.1","subclass":"","packing_group":"III","un":"2811","cas":"133073-73-1","poisons":"","hazardous":"Yes","restricted":"No","epa":"","added_by":"0","carcinogen":null}, {"id":"3","name":"(+)-Biotin 4-Nitrophenyl ester","class":"","subclass":"","packing_group":"","un":"","cas":"33755-53-2","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}, {"id":"4","name":"(+)-Biotin N-hydroxysuccinimide ester","class":"","subclass":"","packing_group":"","un":"","cas":"35013-72-0","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}]

console.log(data); will return [Object, Object, Object]

or if you wanna process as a JSON string you should do this:

const data = '[{"id":"2","name":"<small>L</small>(+)-Amethopterin Hydrate","class":"6.1","subclass":"","packing_group":"III","un":"2811","cas":"133073-73-1","poisons":"","hazardous":"Yes","restricted":"No","epa":"","added_by":"0","carcinogen":null}, {"id":"3","name":"(+)-Biotin 4-Nitrophenyl ester","class":"","subclass":"","packing_group":"","un":"","cas":"33755-53-2","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}, {"id":"4","name":"(+)-Biotin N-hydroxysuccinimide ester","class":"","subclass":"","packing_group":"","un":"","cas":"35013-72-0","poisons":"","hazardous":"No","restricted":"No","epa":"","added_by":"0","carcinogen":null}]'

JSON.parse(data) will return too [Object, Object, Object]

Sign up to request clarification or add additional context in comments.

1 Comment

when I console.log(data) it returns me [Object {id="2", ...}, Object {id="3", name="(+)-Biotin 4-Nitrophenyl ester", ...}, Object {id="4", ...}] so it's in a JSON format, but I can't access it for some reason.
1

Changed the calling function to this:

searchAjax(url: any): any
{   
    let new_data: any;
    return $.ajax({
        url: url,
        type: 'post',
        dataType: "json", 
        async: false

    }).responseText;
} 

The most likely cause was that my variable was that my variable was null at the time of being called because of async.

Comments

1
file Nmae: self.json
    [
          {
            "id": "2",
            "name": "<small>L</small>(+)-Amethopterin Hydrate",
            "class": "6.1",
            "subclass": "",
            "packing_group": "III",
            "un": "2811",
            "cas": "133073-73-1",
            "poisons": "",
            "hazardous": "Yes",
            "restricted": "No",
            "epa": "",
            "added_by": "0",
            "carcinogen": null
          },
          {
            "id": "3",
            "name": "(+)-Biotin 4-Nitrophenyl ester",
            "class": "",
            "subclass": "",
            "packing_group": "",
            "un": "",
            "cas": "33755-53-2",
            "poisons": "",
            "hazardous": "No",
            "restricted": "No",
            "epa": "",
            "added_by": "0",
            "carcinogen": null
          },
          {
            "id": "4",
            "name": "(+)-Biotin N-hydroxysuccinimide ester",
            "class": "",
            "subclass": "",
            "packing_group": "",
            "un": "",
            "cas": "35013-72-0",
            "poisons": "",
            "hazardous": "No",
            "restricted": "No",
            "epa": "",
            "added_by": "0",
            "carcinogen": null
          }
        ]
     $(document).ready(function($) {
            $.ajax({
                url: 'self.json',
                type: 'GET',
                dataType: 'json',
            })
            .done(function(respose) {
                for (var i = 0; i < respose.length; i++) {
                    resText = respose[i].id+' '+respose[i].name+' '+ respose[i].class+' '+respose[i].subclass;
                    console.log(resText);
                };
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });

        });

Output: enter image description here

3 Comments

Turns out that because I was doing ajax, the variable was null when it was displaying :\
Using jQuery Ajax
yeah, I made an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.