Convert array of strings to array of objects in JSON property

chris

I have the following JSON returned from an endpoint

{
    "response": {
        "lines": [
            "[{'line':'3007621h7s2','type':'national'},{'line':'3007663f7s9','type':'international'}]",
            "[{'line':'3007262p7f6','type':'national'},{'line':'3007262a0s1','type':'international'}]"
        ]
    }
}

the property lines is an array that should contain multiple arrays, however, as you can see, lines is an array of strings. How can I make each element in the lines property to be an array of objects?

Thanks

Adam

The easiest way to convert the strings to arrays is to eval() them.

var obj = {
  "response": {
    "lines": [
      "[{'line':'3007621h7s2','type':'national'},{'line':'3007663f7s9','type':'international'}]",
      "[{'line':'3007262p7f6','type':'national'},{'line':'3007262a0s1','type':'international'}]"
    ]
  }
}

obj.response.lines = obj.response.lines.map(line => eval(line));

console.log(obj);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert array of objects to array of strings

Convert array of strings into an array of objects

Convert array of strings to an array of objects by splitting the strings

Convert array in objects to individual strings

Filter array of objects by property with array of strings with an array of strings

Convert array of strings into an array of objects in javascript?

JS : Convert Array of Strings to Array of Objects

How to convert an array of custom objects to an array of strings?

Convert array of strings into array of objects in R

Using jq to convert array of strings into array of objects

Function to convert array of strings into array of objects

Convert array of json objects into json

JSON stringify array of objects with property

How to convert JSON to array of strings

convert list of objects to Json array

Convert JSON into Array of JavaScript Objects

How to convert JSON array to objects

Convert Json objects Collection to Array

Convert a JSON array to a bash array of strings

Go: Convert Strings Array to Json Array String

Convert array of strings into an array of hashes in JSON

Convert an array of objects into a nested array of objects based on string property?

Convert dataframe to a rec array (and objects to strings)

Convert array of elements to array of JSON objects

how to convert an array into a json array objects

Convert json array of objects to bash associative array

Convert an array of objects to a hash based on a property of the object

Dart convert array of json objects to list of objects

Array of strings into array of objects