-2

I have the array which I want to convert it into required json format

Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] [3] => [email protected] [4] => [email protected]  )

required json format

[
    {id:0,text:"[email protected]"},
    {id:1,text:"[email protected]"},
    {id:2,text:"[email protected]"},
    {id:3,text:"[email protected]"},
    {id:4,text:"[email protected]"}
]

I have tried as

var jsonObj = {};
for (var i = 0 ; i < sampleArray.length; i++) {
    jsonObj["id:" + (i+1)] ,"text:" sampleArray[i];
}
console.log(jsonObj);

getting error.How to convert it into required form

6
  • required json format - that's not valid json - put it in a json lint to check Commented Apr 27, 2021 at 15:34
  • const yourNewArray = yourOriginalArray.map((item, index) => { id: index, text: item }) Commented Apr 27, 2021 at 15:39
  • A minor change to your code to get closer your required (but not "JSON") output: jsonObj["id:" + (i+1)] = "text:" + sampleArray[i]; Commented Apr 27, 2021 at 15:40
  • 2
    JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented Apr 27, 2021 at 15:40
  • Why not use PHP to convert the Array to JSON. Please see: php.net/manual/en/function.json-encode.php One issue I see is that your Array, in PHP, is not constructed properly. See more: php.net/manual/en/function.array Commented Apr 27, 2021 at 15:42

2 Answers 2

3

simply map the sampleArray

const sampleArray = ['[email protected]', '[email protected]','[email protected]', '[email protected]', '[email protected]']

var jsonObj = sampleArray.map((el, index) => ({id: index + 1, text: el}))
console.log(jsonObj);

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

Comments

2

For a solution you may be a little more familiar with, here is a fixed version of the loop you tried to use. For each element in the a array I created an object called item and stored in it the desired id for the index of the element in the array and text for the text value stored at that index, I then pushed the item object into an array.

const sampleArray = ['[email protected]', '[email protected]','[email protected]', '[email protected]', '[email protected]']

var jsonObj = [];
for (var i = 0 ; i < sampleArray.length; i++) {
  var item = {id:i,text:sampleArray[i]};
  jsonObj.push(item);
}
console.log(jsonObj);

Additionally, I think that strict JSON requires that an array in JSON is nested in an object though many parsers will accept it this way. Also see @freedomn-n's comment below.

1 Comment

To clarify, my issue was with lack of " around the identifiers. There's also the distinction between a javascript object (as this answer) and "json" (a string) - but that's just JSON.stringify(jsonObj)... see "jsonObj" - makes no sense :) it's like calling a variable var intObj or var intString. Upvoted your answer anyway as it's probably what OP actually wants (same as upvoted other answer with different code, same end result)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.