1

I am a beginner programmer trying to convert JSON array property value from arrays to keys.
From

..,"searchResult":[{"itemId":["123"],"title":["abc"],..}]
to

..,"searchResult":[{"itemId":"123","title":"abc",..}]

Full original JSON result here with search result highlighted

Full original JSON result here

the JSON array is being received in this code

//function to retrieve JSON arrays
function _cb_findItemsByKeywords(root) {
//Navigates and assigns variable "items" into the property, item
  var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || []; 
  var a = (items);
  //assigned variable a to the array
}  

Question: How do I remove the square brackets and check my array a?

EDIT:
Sorry for the confusion, My goal is to combine this array with [..] with another array without [..] before appending the properties to a table. My plan:enter image description here

9
  • What do you want to set these keys to? It's not valid JSON to just have keys. What are you trying to accomplish Commented Jan 25, 2017 at 13:42
  • 5
    {"123"} does not make sense Commented Jan 25, 2017 at 13:42
  • You mean "how to convert a JSON array to an object"? Commented Jan 25, 2017 at 13:43
  • 1
    Well {123} doesn't make any more sense than {"123"}. Commented Jan 25, 2017 at 13:45
  • 1
    "searchResult":[{"itemId":{"123"},"title":{"abc"},..}] is not a valid json format Commented Jan 25, 2017 at 13:45

1 Answer 1

4

If I understand your question correctly, you wish to convert a JSON array into a Javascript object, since altering JSON property values to keys wouldn't be valid JSON.

To convert JSON array into Javascript object, in pure Javascript you do:

JSON.parse(yourJSONgoeshere);

Docs and examples here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Hope it helps.

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

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.