How to get elements after JSON.parse() in mJS

If you are asking a question, please follow this template:

  1. My goal is:
    I want to get element from JSON obj like “ip = jsonObj.ip” after “jsonObj = JSON.parse(text)”.
  2. My actions are: my code is as followings.

let parse = function(response) {
let text = JSON.stringify(response);
print(“Geolocation responseText: “,text);
//text = text.replace(/(<([^>]+)>)/ig,””);
//text = ‘[’ + text + ‘]’;
let jsonObj = JSON.parse(text);
print("Geolocation jsonObj: ",jsonObj);

  print(jsonObj.ip);
  print(JSON.parse(jsonObj.ip));
  ipGeoLoc.ip = jsonObj.ip;
  ipGeoLoc.city = jsonObj.city;

}

  1. The result I see is:
    The error occurred at the line of “print(jsonObj.ip);” and another error also occurred at “print(JSON.parse(jsonObj.ip));”

    The error log is as followings:
    [Jun 2 17:38:31.436] Geolocation responseText: “{\n “ip”: “122.47.26.50”,\n “city”: “Sogongdong”,\n “region”: “Seoul-t’ukpyolsi”,\n “country”: “KR”,\n “loc”: “37.5645,126.9750”,\n “org”: “AS17858 LG POWERCOMM”\n}”

[Jun 2 17:38:31.491] jsonObj: {\x0a “ip”: “122.47.26.50”,\x0a “city”: “Sogongdong”,\x0a “region”: “Seoul-t’ukpyolsi”,\x0a “country”: “KR”,\x0a “loc”: “37.5645,126.9750”,\x0a “org”: “AS17858 LG POWERCOMM”\x0a}

[Jun 2 17:38:31.491] at ip-location.js:26
[Jun 2 17:38:31.492] at ip-location.js:51
[Jun 2 17:38:31.492] at api_http.js:102
[Jun 2 17:38:31.492] at api_net.js:58
[Jun 2 17:38:31.493] MJS callback error: type error

  1. My expectation & question is:
    The errored code of “print(jsonObj.ip);” or “print(JSON.parse(jsonObj.ip));” should output “122.47.26.50”.

I suspect that mJS dose not support access such as jsonObj.ip then please let me know how to get the elements in jsonObj.

It is very hard to figure out where the problem actually is - your example code is very confusing and unclear. Please tidy up, make it clear and minimal.

My code in ip-geoloc.js is as followings:

The error log of above function code:

please let me know how to get the element in JSON object on mJS.
and please show me the example.

You don’t need JSON.stringify(response). The respnse is already a json string.
Just JSON.parse(response)

1 Like

Thanks, it works well.

1 Like