-2

I'm trying to convert the following JSON string into an object:

{
    "purpose": "This is test",
    "suffix": "test_suffix",
    "tags": {
        "environment": "development",
        "provider": "Test"
    },
    "new_tag": "1234",
    "disk": "{data0 = { size = \"128\", caching = \"None\", type = \"Premium_LRS\" }",
    "customer": "1234"
}

How can I do so?

3x071c
  • 955
  • 10
  • 40
Leapahead
  • 1
  • 1
  • If you have code, show code. Right now, it's entirely unclear what you're showing: it looks like it's already similar to JSON, but there is no `=` in JSON, so what generated this? Because the quotes-in-quotes suggest this isn't real data at all. Show what you _actually_ have (and, show the code you've already written to do what you wanted to do) – Mike 'Pomax' Kamermans Mar 28 '21 at 21:01
  • I guess you are looking for a JSON.pase(your_string) – Vinicius Silveira Alves Mar 28 '21 at 21:04
  • Welcome to StackOverflow. It is really important not to assume we can draw the same conclusions or make the same assumptions as you. For instance, you state that is a string, but the first character is `{`. JavaScript strings don't start with that - so what is it really? Additionally, you are trying to correct something that was created incorrectly - or at minimum very poorly - at the source. Fix the source so it produces a valid JSON string rather than attempting to have the recipient fix the source's error. Good luck to you. – Randy Casburn Mar 28 '21 at 21:17

1 Answers1

-1

Normally you could use JSON.parse(). But since you have fields like: "tags":"{environment = "development", provider = "Test" }". You will have to parse every string "{environment = "development", provider = "Test" }" manually.

You haven't specified if you want those strings as a string value or parse them to JSON element, but in both cases you will have to deal with quotes because right now this is not even one string but something like:

"tags" : "{environment = " development ", provider = " Test " }"

Jake White
  • 90
  • 1
  • 8