0

I'm receiving a stringified Array of Objects in JS:

"[{version:07/18,test:true}]"

How can I get this back to an Array I can work with in JS? I've tried JSON.parse but this don't worked:

var abc = "[{version:07/18,test:true}]";

console.log(JSON.parse(abc));

Thanks for helping me!

8
  • 3
    JSON.parse didn't work because that isn't valid JSON. I'd say the issue worth solving is whatever is sending you data in such an awful format :) Commented Aug 14, 2019 at 15:22
  • 2
    Where does that string come from? Maybe the source can be modified to provide valid JSON, I.E. '[{"version":"07/18","test":true}]'. Commented Aug 14, 2019 at 15:23
  • @TylerRoper Sometimes you don't have your hands in other companies stuff :) I need to work with this somehow. Commented Aug 14, 2019 at 15:24
  • I'd say you need more of an attempt, personally, as there's a fair bit of complexity here. For example, even if you eval this (boo hiss), it's still not right; e.g. { version: 07/18 } will produce a giant decimal. Commented Aug 14, 2019 at 15:25
  • 2
    @Mr.Jo I would handle this by contacting that company "Hey, just FYI, you guys have a bug somewhere and are producing invalid JSON." Commented Aug 14, 2019 at 15:27

3 Answers 3

1

As the stringified Array of Objects you have isn't a valid JSON string, the JSON.* functions can't deal with it.

A solution is to create your own parser that deals with the type of strings you have and returns an array of objects created from those in the string.

I created a one for you :

/**
* a function designed to parse strings like the one you have
* it can deal with multiple objects in that string.
* @param arrLikeStr a string that as the one you have. Begins with "[" and ends with "]" and as much objects enclosed in "{" and "}" and separated with ",".
**/
const populateArrray = arrLikeStr => {
  /** remove "[", "]" and spaces **/
  arrLikeStr = arrLikeStr.replace(/[\[\]\s]/g, '');
  /** split the string based on '},' to get the objects in that string and loop through them **/
  return arrLikeStr.split('},').map(item => {
    /** represents the current object (item) that is parsed **/
    const obj = {};
    /** remove "{" and "}" from the current item **/
    /** split it based on "," and loop through **/
    item.replace(/[{}]/g, '').split(',').forEach(key => {
     /** split based on ":" to get the objects's attributes **/
      key = key.split(':');
      /** construct the object **/
      /** "true" and "false" are represented as boolean true and false **/
      obj[key[0]] = ['true', 'false'].indexOf(key[1]) !== -1 ? Boolean(key[1]) : key[1];
    });
    /** return the object after constructing it **/
    return obj;
  });
};

/** tests **/

const str = "[{version:07/18,test:true},{version:17/03,test:false}]",
  arr = populateArrray(str);

/** print the whole parsed array **/
console.log(arr);

/** print the second object in the parsed array **/
console.log('second object:');
console.log(arr[1]);

/** print the version of the first object in the parsed array **/
console.log('first object version:');
console.log(arr[0].version);
.as-console-wrapper {
  min-height: 100%;
}

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

Comments

1

If you can't fix the source you aren't left with any great options. You could use a library such as relaxed-json to parse JSON with missing quotes on keys, but it doesn't handle the 07/18 so you will need to quote that first.

const str = "[{version:07/18,test:true}]";

// Quote strings in the format 07/18:
const quoted = str.replace( /\d+\/\d+/g, '"$&"' );

// Parse with relaxed-json
const result = RJSON.parse( quoted );

console.log( result[0].version );
console.log( result );
<script src="https://cdn.jsdelivr.net/npm/[email protected]/relaxed-json.min.js"></script>

This is fragile code, that might work now for what you need, but will stop working if the source ever includes any other weird values that don't match the \d+/\d+ pattern. I recommend that if you use something along these lines, you treat it as a temporary solution, and keep putting pressure on the maintainer of the data source to return their data as JSON.

Comments

0

hope this approach works with you

replace each , by },{ then convert the string into array using split method (",")

// original string : "[{version:07/18,test:true}]"

var arr;
abc = abc.replace("," , "},{"); // "[{version:07/18},{test:true}]"
arr = abc.split(","); // [{version:07/18},{test:true}]

then you can parse each element in the array alone

JSON.parse(arr[0]); // {version:07/18}
JSON.parse(arr[1]); // {test:true}

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.