2

I want to store an object array in a SP field and then inject it into a standard statement for interrogation of its properties. I have a field named User that contains the following:

firstName:"John", lastName:"Doe", age:46, bornIn:"Utica", bornOn:"July, 10 1943"

this assumes I have pulled the field from the list and itemUser = listItem.get_item('User');

the code on the page is:

var person = [];
person = {itemUser};
person["firstName"];

this generates a compile error that it wants a :

if I replace itemUser with the actual string - firstName:"John", lastName:"Doe", age:46, bornIn:"Utica", bornOn:"July, 10 1943" - it works

my question is how to store an object array in a field and then pass it to that code so that you can use standard methods to interrogate the properties?

2
  • You cannot store direct array to the field of the SharePoint list. You can store your data as string in "Multil-line text" (Note) field. And retrieve these data and convert them in JSON for further operations. I think this workaround will help you. Jul 29, 2017 at 4:19
  • Dikesh thank you for you response. I did change my field type from single line to to multi-line. Then added the object as follows:{"firstName":"John","lastName":"Doe","age":46,"bornIn":'Utica","bornOn":"July, 10 1943"} Then i applied a JSON.parse to it but it still did nor render and [object,Object] Any other suggestions?
    – Etswatch
    Jul 31, 2017 at 18:58

2 Answers 2

2

Store your string in multi-line text with "Text Only" data type. After that fetch and get your string and parse it into JSON.

Your string in SharePoint list, we fetch and store it in variable:

var objStr = '{"firstName":"John","lastName":"Doe","age":46,"bornI‌​n":"Utica","bornOn":‌​"July, 10 1943"}';

var objJSON = jQuery.parseJSON(objStr); //Parse into JSON

Now, use objJSON object as JSON object.

1
  • @Estwatch Please mark the answer so it will help others. Aug 2, 2017 at 3:56
2

You need to JSON.stringify your object to store it and then JSON.parse to parse the string back to object

3
  • John, thanks. I'm not sure what you mean by storing it. For development I used the JSON.stringify to convert a string and then copy and pasted that value into the field but then using the JSON.parse did not work - still treated as a string did not create the object. The cconversion pulls a double quote before the curly brace which i why it fails. Any other suggestions or ideas of what I may be doing wrong?
    – Etswatch
    Jul 31, 2017 at 19:49
  • @Etswatch so its a string like ""str"" ? Is that the case?
    – JohnPan
    Aug 1, 2017 at 7:29
  • It did work. Since I was cutting and pasting data from one field into another the field rather than actually saving it, I believe that was causing the issue. Once I created the object string in in sublime, pasted it into the field and then JSONparse the extracted string, worked like a top. What it was doing was creating an escaped version of the text \ in from of all " and " at the begining and end of the string. Could of been user error as well, but glad I got it working. Thank you all
    – PianoMan
    Aug 2, 2017 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.