0

I have an object with nested arrays which I:

  1. First turn into string using angular.toJSON() as ngCSV doesn't support nested arrays
  2. Then export as CSV file using ngCSV
  3. Then import and parse into JSON again using PapaParse.

The initial array looks like this:

filteredRecords [
{
date: '', 
time: '', 
comments: [
    {message: '',
    commenttime: ''},
    {message: '',
    commenttime: ''},
    {message: '',
    commenttime: ''}
],
arrival: '',
},
{
date: '', 
time: '', 
comments: [
    {message: '',
    commenttime: ''},
    {message: '',
    commenttime: ''},
    {message: '',
    commenttime: ''}
],
arrival: '',
}
]

After I've used angular.toJson() and done the export/import, the comment property looks like this:

filteredRecords [
{
date: '', 
time: '', 
comments: "[{"message":"wopwop","commenttime":"10.46"},{"message":"checkingin","commenttime":"10.46"},{"message":"tripletest","commenttime":"10.46"}]",
arrival: '',
}, ...
]

My problem is that I need to reverse the process and turn this string into an array after it's parsed, as my app needs to be able to read comment.message and comment.commenttime.

Any idea on how to proceed?

EDIT

I've managed to manually parse the string back into array using the solution from lex82, but I don't know how I could iterate into the main array and automatically do the same for all comments objects in every object from the array.

Any tips?

1 Answer 1

1

It looks like you need JSON.parse(). I think this should work:

var yourArray = JSON.parse(yourString);

You find a more elaborate answer here.

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

2 Comments

lex82, it works but only manually! How could I automatize it for every comments object of every object in the array?
I'm not sure if I understand your question. What do you mean with manually? Can you post some code how you do it manually? Can't you just loop over the array and do it for every element?

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.