-2

Would it be possible to parse a string as follows… {"Key": "Value"} back into a object? When I ever I try to parse it, I receive errors. I've been trying to use json.parse, but that does not work either.

This is what i have mapped out, but for this format it fails.

// Creating a list of objects, while mapping properties of an object
let obj = "{
  "key": "3"
}";
let objList = Object.entries(obj).map(([key, value]) => Object.fromEntries(new Map([
  [key, value]
])));
bitznbytez
  • 74
  • 1
  • 13
  • 4
    hmm.. u mean like `JSON.parse("{'key':'value'}")`? – Mauricio Sipmann Aug 02 '19 at 14:01
  • _"I've been trying to use `json.parse`, but that does not work either."_ - can you post your attempts at that approach, along with issues you've had, since that would be the normal thing to do. – James Thorpe Aug 02 '19 at 14:05
  • That's not a string, that's already an object. – Dave Newton Aug 02 '19 at 14:05
  • Your code doesn't include the string you mentioned. It's really unclear what you are trying to do. – Quentin Aug 02 '19 at 14:05
  • In your example only the values must be parsed, that's what you're looking for ? – Fraction Aug 02 '19 at 14:10
  • @DaveNewton if that's an object, then I dont get why it does not work in the code snippet? – bitznbytez Aug 02 '19 at 14:14
  • @Fraction yes, basically I want the keys and values from this string – bitznbytez Aug 02 '19 at 14:15
  • @MauricioSipmann That would work if it were in that format, but its actually "{"key":"value"}" – bitznbytez Aug 02 '19 at 14:19
  • 2
    That by itself isn't valid syntax. Do you mean you have a string that contains `{"key":"value"}` or do you have a string that contains `"{"key":"value"}"`. You can't directly put `"{"key":"value"}"` in your source, as that's not valid, but you _could_ have a string that contains that value. Can you edit your question to show precisely what you're working with? – James Thorpe Aug 02 '19 at 14:22
  • @bitznbytez I updated my answer in reply of your edited question. – JustAnotherDeveloper Aug 02 '19 at 14:25
  • @JamesThorpe I actually am getting that string returned from an api call in that exact format. Any suggestions? Something to be addressed from the server side maybe? – bitznbytez Aug 02 '19 at 14:26
  • If the server is returning exactly `{"key": "3"}` it is enough JSON.parse. – JustAnotherDeveloper Aug 02 '19 at 14:27
  • @bitznbytez If you execute your code you'll get a `SyntaxError`, you're sure that your json is not like: `"{ \"key\": \"3\"}"` ? – Fraction Aug 02 '19 at 14:28
  • 1
    In what format (I specified two)? Using `"` inside the string around keys/values and is also surrounded by `"`? If so, then yes - this is an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) where the solution is to go ask (or fix if it's yours) the server response to be valid JSON. – James Thorpe Aug 02 '19 at 14:36

1 Answers1

1
let obj = "{
  "key": "3"
}";

This is incorrect: if you want to write a multiline string, you have to put a \ at the end of the line:

let obj = "{\
    "key": "3"\
}";

But there is another error: you have to escape the ":

let obj ="{\
    \"key\": \"3\"\
}";

You could use ' for enclosing string instead of " so you don't need the escape:

let obj = '{\
    "key": "3"\
}';

Anyway this is a little string, you could write it in one line:

let obj = '{"key": "3"}';

Now you can use JSON.parse.

let obj = '{"key": "3"}';
console.log( JSON.parse(obj) );