3

I have an array which is in string format,

var str = { 
    id: 123,
    changes: "[[atr:test1, old:null, new:null], [atr:messageText, old:test here, new:hello test], [atr:status, old:null, new:1]]"
}
var d = str.changes

I tried to convert the 'changes' array from string format using different methods by combining split(), replace(), slice() etc...and even JSON.parse(), but nothing worked.

Is there any way to convert this into javascript array?

2
  • This "string format" array is JSON. So think of the problem as converting JSON to an object. Research JSON in the MSN javascript documentation and look for parse and stringify methods. Commented May 13, 2020 at 6:44
  • i think the literal in changes is not in valid JSON array syntax. Commented May 13, 2020 at 6:49

2 Answers 2

2

Note that the string is not valid anything but string. It is not a valid array, and the string is not valid JSON.

If you can, get the server to change it to the valid JSON string

"[{\"atr\":\"test1\", \"old\":null, \"new\":null}, {\"atr\":\"messageText\", \"old\":\"test here\", \"new\":\"hello test\"}, {\"atr\":\"status\", \"old\":null, \"new\":1}]"

If the response is ALWAYS on the format you gave, then you can create valid JSON

var str = {
  id: 123,
  changes: "[[atr:test1, old:null, new:null], [atr:messageText, old:test here, new:hello test], [atr:status, old:null, new:1]]"
}

// change the inner [ ] to { }
let changes = str.changes.replace(/\[\[/g, "[{").replace(/\], \[/g, "},{").replace(/\]\]/g, "}]")

// change the unquoted keys and values to quoted keys and values
changes = changes.replace(/(\w+):/g, '"$1":').replace(/:([\w ]+)([},])/g, ':"$1"$2')

// parse the object
changes = JSON.parse(changes);

// replace "null" with null - could have been done above bt the regex would be nasty
changes.forEach(item => Object.keys(item).forEach(key => item[key] = item[key] === "null" ? null : item[key]))

console.log(changes)

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

Comments

0

I think the problem is that the key 'changes' do not any valid JSON. You can validate, format it here.

If there is a valid JSON in 'changes' key, It can be converted to Js array using JSON.parse();, Something like:

    var str = { id: 123,
            changes: `[
  [
    {
      "atr": "test1",
      "old": null,
      "new": null
    }
  ],
  [
    {
      "atr": "messageText",
      "old": "test here",
      "new": "hello test"
    }
  ],
  [
    {
      "atr": "status",
      "old": null,
      "new": 1
    }
  ]
]`
          }
var d = JSON.parse(str.changes);

    console.log(d);

//str.changes Object:
[[[object Object] {
  atr: "test1",
  new: null,
  old: null
}], [[object Object] {
  atr: "messageText",
  new: "hello test",
  old: "test here"
}], [[object Object] {
  atr: "status",
  new: 1,
  old: null
}]]

Comments

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.