-2

so here is the problem : I am passing a list/array of objects from front end to my node js back end.

This is how it looks on the front end : [{"sample1": "this", "apple2" : eat},{"sample2": "thisIs2", "apple3" : eatFruits}]

but when it is passed to the backend , its converted to String,something like this:
"[{"sample1": "this", "apple2" : eat},{"sample2": "thisIs2", "apple3" : eatFruits}]"

So when I run a for loop in my node js, the length is calculated as each characters. what do i do to get the pure array and the contents in the array. Thanks

  • 3
    Does this answer your question? [Converting a string to JSON object](https://stackoverflow.com/questions/10976897/converting-a-string-to-json-object) – lusc Oct 26 '22 at 11:26
  • 1
    Use `JSON.parse("[{"sample1": "...)` – ask4you Oct 26 '22 at 11:26
  • @ask4you Anytime I use that, it throws this error .. SyntaxError: Unexpected token t in JSON at position 2 – that_flutter_guy Oct 26 '22 at 11:34
  • @lusc It didn't help. I tried already, I used JSON.parse, it throws this error ... SyntaxError: Unexpected token t in JSON at position 2 – that_flutter_guy Oct 26 '22 at 11:36
  • Sounds like you're incorrectly stringifying it then. You'll need to debug how it's being sent. The string you've posted is indeed invalid due to missing quotes around the object values `eat` and `eatFruits`, but that could easily be a typo on your part – pilchard Oct 26 '22 at 11:36
  • @pilchard here is what I'm doing.. var q = JSON.parse(req.body.toppings) console.log(typeof(req.body.toppings))---> the type shows String – that_flutter_guy Oct 26 '22 at 11:38
  • but what about on the front end? How are you encoding it before sending. – pilchard Oct 26 '22 at 11:40
  • @pilchard here is how it is sent from flutter... ``var newObject = { "topping": "${selectedItems[i].toppings['topping']}", "price": "${selectedItems[i].toppings['price']}", "quantity": "${selectedItems[i].quantity}" }; newMapToppingsAsList.add(newObject); print(newObject);`` – that_flutter_guy Oct 26 '22 at 11:41
  • @pilchard i'm sending it as A multipart request in flutter. Is that right? – that_flutter_guy Oct 26 '22 at 11:44
  • You need to appropriately stringify it see: [how to stringify json in flutter](https://stackoverflow.com/questions/54487659/how-to-stringify-json-in-flutter) – pilchard Oct 26 '22 at 11:55
  • @that_flutter_guy Yea, i think because `eat` and `eatFruit` are functions/classes which can't be parsed or stringified with `JSON`. – ask4you Oct 26 '22 at 12:16

1 Answers1

0

"[{"sample1": "this", "apple2" : eat},{"sample2": "thisIs2", "apple3" : eatFruits}]"
Your object is the "stringified" JSON. Means whole JSON object represented as string

e.g.

const jsonObj = [{"id": 1, "name": "John"}, {"id": 2, name: "Jane"}];
const strigified = JSON.stringify(jsonObj);
console.log(strigified);

output

'[{"id":1,"name":"John"},{"id":2,"name":"Jane"}]'

This can be reversed by parsing this string as JSON

const original = JSON.parse(strigified);
console.log(original);

output

[{"id": 1, "name": "John"}, {"id": 2, name: "Jane"}]

JSON.strigify() & JSON.parse() are exactly reciprocal methods...

TalESid
  • 2,304
  • 1
  • 20
  • 41