0

I have the following text in form of var in JS which i am getting from another function.

    var text = '["{"a1":"zxcv","a2":"pqrs","c2":[1,2,3],"a3":{"aa3":"asdfgh","aa5":null}}","{"a1":"xyz","a2":"mno","c2":[103],"a3":{"aa8":"qwerty"}}"]';

I have a requirement to check the count of a1, a2, c2 and it's values and a3 and its values as well. such as: a1: 2, a2: 2, c2: 4, a3: 3 and so on (count of child elements as well)

The crude way i could think to get my result was to 1st remove the first and last " and replace }","{ with },{ This gave me a json array of objects and using JSON.parse gave me a better structure converted then i could traverse easily on that. I couldn't find any library or other solution as alternative to this.

 var text = '["{"a1":"zxcv","a2":"pqrs","c2":[1,2,3],"a3":{"aa3":"asdfgh","aa5":null}}","{"a1":"xyz","a2":"mno","c2":[103],"a3":{"aa8":"qwerty"}}"]';

        console.log(text);

        text = text.replace(/\["{/g, "[{"); // remove first double quote
        text = text.replace(/\}"]/g, "}]"); // remove last double quote
        text = text.replace(/\}","{/g, "},{"); // replace middle quotes

        console.log(text);

        var formattedText = JSON.parse(text);

        console.log(formattedText);

Expected output after i get it in a object form as then i can loop over object and use counter to maintain count:

a1: 2, a2: 2, c2: 4, a3: 3

Is there any function (inbuilt or with a library) that can help me with this?

11
  • 2
    Is it possible to change the input format of the string? If you could change it to valid JSON this problem could be solved in a single method call. \/ \/ \/ what he said :) Commented Jun 19, 2019 at 10:54
  • 2
    Or you could ask the source to provide you a valid JSON string, edit: What he said ^^^ Commented Jun 19, 2019 at 10:55
  • 2
    Why are you getting not-JSON that you have to manually try to mangle into JSON? Commented Jun 19, 2019 at 10:56
  • 1
    are you sure isn't it better to just fix the JSON source? Commented Jun 19, 2019 at 10:56
  • 3
    I'm not aware of any library that takes invalid JSON and magically formats it into valid. The whole idea of having JSON as a standard is to not have to do that. Commented Jun 19, 2019 at 11:02

1 Answer 1

2

You can try fixing that string by removing quotes that are not key/value delimiters and parse the result as a json:

var text = '["{"a1":"zxcv","a2":"pqrs","c2":[1,2,3],"a3":{"aa3":"asdfgh","aa5":null}}","{"a1":"xyz","a2":"mno","c2":[103],"a3":{"aa8":"qwerty"}}"]';


t = text
    .replace(/"([^"]+)":/g, '@$1@:')
    .replace(/:"([^"]+)"/g, ':@$1@')
    .replace(/"/g, '')
    .replace(/@/g, '"')


console.log(JSON.parse(t))

If you're interested in what these replacements do exactly, here's a step-by-step visualization:

var text = '["{"a1":"zxcv","a2":"pqrs","c2":[1,2,3],"a3":{"aa3":"asdfgh","aa5":null}}","{"a1":"xyz","a2":"mno","c2":[103],"a3":{"aa8":"qwerty"}}"]';

String.prototype.show = function(s) {
  console.log(s + ": " + this);
  return this;
}


t = text
    .show('Init')
    .replace(/"([^"]+)":/g, '@$1@:')
    .show('Step 1')
    .replace(/:"([^"]+)"/g, ':@$1@')
    .show('Step 2')
    .replace(/"/g, '')
    .show('Step 3')
    .replace(/@/g, '"')
    .show('Step 4')

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

1 Comment

george, thanks for suggestion, works well, but i'm having a bit of confusion understanding the regex that you have used. Can you explain it a bit?

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.