0

I am trying to iterate each element of json but fails to print. i tired JSON.parse() too.

code:

var j = "{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:16.881\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":527,\"Message\":\"SyncData: Billing Software SyncData() called\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.060\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":38,\"Message\":\"SyncData: Start\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.111\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":44,\"Message\":\"SyncData: LocalConnectionString = Data Source=PARAM\\\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;\"}";  
for(var key in j){
    console.log(j[key]);
}

above code prints each character like enter image description here

2
  • 2
    It's the classical confusion between the JSON data format and JavaScript objects. You have a string and you never parse it as JSON so you're just looping its characters. Commented Oct 12, 2018 at 9:03
  • What @ÁlvaroGonzález said is true, plus after you used JSON.parse() you most likely want to loop on Object.keys(j), not on j directly. Commented Oct 12, 2018 at 9:04

3 Answers 3

2

What I noticed when I formatted your string was this

{
 "Type": "INFO ",
  "TimeStamp": "2018-10-10 12:05:16.881",
  "IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
  "MacId": "84:7B:EB:3F:65:A4",
  "OS": "Microsoft Windows 10 Home Single Language",
  "Method": "SyncData",
  "LineNo": 527,
  "Message": "SyncData: Billing Software SyncData() called"
}\n{
  "Type": "INFO ",
  "TimeStamp": "2018-10-10 12:05:17.060",
  "IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
  "MacId": "84:7B:EB:3F:65:A4",
  "OS": "Microsoft Windows 10 Home Single Language",
  "Method": "SyncData",
  "LineNo": 38,
  "Message": "SyncData: Start"
}\n{
  "Type": "INFO ",
  "TimeStamp": "2018-10-10 12:05:17.111",
  "IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
  "MacId": "84:7B:EB:3F:65:A4",
  "OS": "Microsoft Windows 10 Home Single Language",
  "Method": "SyncData",
  "LineNo": 44,
  "Message": "SyncData: LocalConnectionString = Data Source=PARAM\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;"
}

To work with this you can do something like this

const j = "{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:16.881\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":527,\"Message\":\"SyncData: Billing Software SyncData() called\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.060\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":38,\"Message\":\"SyncData: Start\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.111\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":44,\"Message\":\"SyncData: LocalConnectionString = Data Source=PARAM\\\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;\"}";  

//first remove the \n and join with a comma ,
// enclose your multiple object within an array
const joinedWithComma = "[" + j.split('\n').join(',') + "]"


//now parse this
const parsedValue = JSON.parse(joinedWithComma)


console.log(parsedValue)

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

7 Comments

Even when the OP has marked your answer as the solution, it's not. OP was trying to iterate properties from a string, and your answer says nothing about this. Both the other one and mine does it already...
@MatíasFidemraizer well I figured out that once the string was parsed, the OP would be able to iterate over it ?
@MatíasFidemraizer and I also believe that the question states that the OP needs to parse the string:- "unable to parse json object"
I don't think so. I really believe the OP wanted a solution to avoid putting his own effort... The problem explained in the whole question is about confusing a string with an object, and also - but just a detail - the JSON was invalid. I don't expect someone to ask to turn us into a human JSON linter, anyway :D
@MatíasFidemraizer the string presented seems like a response sort of string; from an API call or from other sources. The title of the question as well as mentioning failed JSON.parse(), I could only infer that the OP might be asking for a way to parse these 'type' of strings.
|
1

Obviously you're iterating characters of a given string. That string is in JSON format, but it's still an array of characters.

Also, your JSON isn't valid. After cleaning it up, I've noticed that it's a JSON array of JSON objects:

const os = JSON.parse(`[{
		"Type": "INFO ",
		"TimeStamp": "2018-10-10 12:05:16.881",
		"IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
		"MacId": "84:7B:EB:3F:65:A4",
		"OS": "Microsoft Windows 10 Home Single Language",
		"Method": "SyncData",
		"LineNo": 527,
		"Message": "SyncData: Billing Software SyncData() called"
	},
	{
		"Type": "INFO ",
		"TimeStamp": "2018-10-10 12:05:17.060",
		"IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
		"MacId": "84:7B:EB:3F:65:A4",
		"OS": "Microsoft Windows 10 Home Single Language",
		"Method": "SyncData",
		"LineNo": 38,
		"Message": "SyncData: Start"
	},
	{
		"Type": "INFO ",
		"TimeStamp": "2018-10-10 12:05:17.111",
		"IP": "2001:0:9d38:90d7:804:3589:8a3c:e43a",
		"MacId": "84:7B:EB:3F:65:A4",
		"OS": "Microsoft Windows 10 Home Single Language",
		"Method": "SyncData",
		"LineNo": 44,
		"Message": "SyncData: LocalConnectionString = Data Source=PARAM\\\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;"
	}
]`)


for (let o of os)
  for (let key in o)
    console.log(key, ' = ', o[key])

2 Comments

@JunedAnsari There's no by to autofix it :D I've fixed it by hand, there's no other way to do it.
@JunedAnsari Clarification: as some other answer points out, when I've said no autofix I mean that you need to implement the fix yourself. In my case, for the sake of answering you, I fixed it manually. Anyway, I believe that fixing JSON is out of the scope of your question.
1

You should parse the string and convert to a JSON object using the method JSON.parse().

EDIT

I noticed that your string has several JSON objects, so you should split the string by \r\n before passing to JSON.parse method.

var j = "{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:16.881\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":527,\"Message\":\"SyncData: Billing Software SyncData() called\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.060\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":38,\"Message\":\"SyncData: Start\"}\r\n{\"Type\": \"INFO \",\"TimeStamp\":\"2018-10-10 12:05:17.111\",\"IP\":\"2001:0:9d38:90d7:804:3589:8a3c:e43a\",\"MacId\":\"84:7B:EB:3F:65:A4\",\"OS\":\"Microsoft Windows 10 Home Single Language\",\"Method\":\"SyncData\",\"LineNo\":44,\"Message\":\"SyncData: LocalConnectionString = Data Source=PARAM\\\\MSSQL2K8r2;Initial Catalog=HQPharma;User ID=sa;Password=developer;\"}";
var list = j.split('\r\n');
for (var i = 0; i < list.length; ++i) {
  var obj = JSON.parse(list[i]);
  for(var key in obj) {
    console.log(obj[key]);
  }
}

4 Comments

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data on line 10
Like Matias said in its answer, the JSON you provided is not valid. I didn't check that. That's the reason for the error.
It's not valid and it's not an object, but an array of objects! :D
Yeah, forgot to change the log of j[key] to obj[key]. Now it works.

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.