-2
"{INDIA=99, PAKISTAN=30}" 

i am having string in this form , want to convert into json object. Have tried

JSON.parse({INDIA=99, PAKISTAN=30}).

but getting errors

VM2602:1 Uncaught SyntaxError: Unexpected token O in JSON at position 1

7
  • 2
    What about this: JSON.parse('{"INDIA":99, "PAKISTAN":30}') Commented Jul 28, 2017 at 13:20
  • 1
    That string is not JSON so can't be parsed as such. Parse it manually or replace() whats needed to make it look like JSON (ymmv doing that). Commented Jul 28, 2017 at 13:21
  • If your string really does look like that, can you go back to wherever it's coming from and alter the output? If you can make it standard JSON, your job will be much easier. Commented Jul 28, 2017 at 13:22
  • Would following work for you ?JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse("{INDIA=99, PAKISTAN=30}" ); Commented Jul 28, 2017 at 13:23
  • @Amit this is not a valid JSON Commented Jul 28, 2017 at 13:25

6 Answers 6

3

There are 2 issues with your string:

  1. Keys and values are separated by colon(:) and not by equals to(=).
  2. Keys should be wrapped in quotes.

Though the best way would be to rectify the JSON(input string) string itself, you can try this approach to create an object from current string.

Note: This is not a robust solution. Since this solution is relying on commas in string to split, if this pattern is available in a value like {India=100, 000, 000}, this solution will fail. So treat this as your last resort. String manipulation will never be 100% covered.

var str = "{INDIA=99, PAKISTAN=30}";
var kv = str.substring(1, str.length -1);

var list = kv.split(", ");
var result = list.reduce(function(p,c){
  var parts = c.split('=');
  p[parts[0]] = parts[1];
  return p;
}, {})
console.log(result)

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

1 Comment

Great solution. Might suit his needs if he cannot alter the original content.
2

Your input isn't valid JSON. You can try to transform it into an appropriate string and then call JSON.parse as you tried. See snippet below and feel free to ask if you have any further questions.

const str = "{INDIA=99, PAKISTAN=30}";
const result = JSON.parse(str.replace(/\=/g, ':').replace(/([a-zA-Z]+)/g, '"$1"'));

console.log(result);
console.log(result['INDIA']);

Comments

1

Try with replace and split the string .And create the object with Array#forEach

var str ="{INDIA=99, PAKISTAN=30}" ;
var c = str.replace(/[\}\{]/g,'').trim().split(',')
var res={};
c.forEach(a=> res[a.split('=')[0].trim()] = a.split('=')[1].trim())
console.log(res)

Comments

0

You are not supplying valid JSON.

In order for JSON.parse() to work, you need to supply a valid JSON.

Example:

{"INDIA": 99, "PAKISTAN": 30}

in your code:

JSON.parse('{"INDIA": 99, "PAKISTAN": 30}');

Hope this helps!

Comments

0

This

JSON.parse('{"INDIA":99, "AKISTAN":30}')

Comments

0

You need to have characters around the key, otherwise it won't read. So:

var str = '{"INDIA":99, "PAKISTAN":30}'
JSON.parse(str);

That should work.

2 Comments

there is = not : in between
I do believe it is supposed to be ':'

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.