-4

I have my responseJson like this

const responseJson = [
      {
        device_id: "arena-FnVq4HTwtBg6JqqBxWBB7W",
        timestamp: "2020-02-10T20:52:00.000Z",
        data: "{"type": "DATA", "unitID": "arena-FnVq4HTwtBg6JqqBxWBB7W",
        "version": "1.0.0",
        "voltage": [130.0222, 129.9743, 129.9567, 129.9853, 130.0043, 130.0257, 130.067,
         130.1005, 130.0698, 130.1056],
         "frequency": [60.0005, 60.0001, 59.9984, 59.9999, 59.9992, 59.9971, 59.9977,
         59.9979, 59.999, 59.9985],
         "timestamp": 1581367920000,
         "phaseAngle": [2.5766, 2.5769, 2.5768, 2.5761, 2.5754, 2.574, 2.5718, 2.5706,
          2.569, 2.5677]}"
      }
    ]

my data is a string not an object. How can i parse my data to get this

const responseJson = [
      {
        device_id: "arena-FnVq4HTwtBg6JqqBxWBB7W",
        timestamp: "2020-02-10T20:52:00.000Z",
        data: {"type": "DATA", "unitID": "arena-FnVq4HTwtBg6JqqBxWBB7W",
        "version": "1.0.0",
        "voltage": [130.0222, 129.9743, 129.9567, 129.9853, 130.0043, 130.0257, 130.067,
         130.1005, 130.0698, 130.1056],
         "frequency": [60.0005, 60.0001, 59.9984, 59.9999, 59.9992, 59.9971, 59.9977,
         59.9979, 59.999, 59.9985],
         "timestamp": 1581367920000,
         "phaseAngle": [2.5766, 2.5769, 2.5768, 2.5761, 2.5754, 2.574, 2.5718, 2.5706,
          2.569, 2.5677]}
      }
    ]

I have tried JSON.parse but no help Here is what i get when i console.log console.log(responseJson.data) throws undefined console.log(JSON.parse(responseJson.data)) throws C:\Users\aravi\Desktop\React\Reactnative\BrixonApplication_App\node_modules\react-native\Libraries\Core\ExceptionsManager.js:94 SyntaxError: Unexpected token u in JSON at position 0

Aravind Reddy
  • 353
  • 4
  • 18
  • Does this answer your question? [Convert JS object to JSON string](https://stackoverflow.com/questions/4162749/convert-js-object-to-json-string) – Kevin Hernandez Feb 11 '20 at 16:24
  • 1
    Looks like whatever is serving that JSON is encoding the data property and then again encoding the whole object. Fix that server-side. – James Feb 11 '20 at 16:25
  • if your first snippet of code is really like that, then your `responseJson` is invalid and should throw and error, since `data` value has a syntax error – Calvin Nunes Feb 11 '20 at 16:26
  • You need to assign this `JSON.parse(responseJson[0].data)` to data. you need to loop if you have more than one object. – Sameer Reza Khan Feb 11 '20 at 16:28
  • @calvin I don't think that's invalid until string itself is a valid json object or array. – Sameer Reza Khan Feb 11 '20 at 16:30
  • ```console.log(responseJson.data)``` thows undefined and ```console.log(JSON.parse(responseJson.data))``` throws errors – Aravind Reddy Feb 11 '20 at 16:34
  • 1
    Internal double quotes should be escaped or should be single quote to make it valid json – Sameer Reza Khan Feb 11 '20 at 16:36
  • @CalvinNunes closed the question ? – Aravind Reddy Feb 11 '20 at 16:53
  • I have edited it – Aravind Reddy Feb 11 '20 at 16:55
  • ok, voted to reopen. But lets go: `responseJson` is an array, not object, so it has no `data` property/key, you can access the first object by `responseJson[0]` as mentioned in some comment above, that's why you get undefined. Second error: you are trying to parse "undefined" with JSON.parse, that's why you get "Unexpected token u". – Calvin Nunes Feb 11 '20 at 16:59
  • I have only one object and i tried either ways and now ```responseJson[0]``` this gives ```TypeError: Cannot read property '0' of undefined``` – Aravind Reddy Feb 11 '20 at 17:01
  • are you absolutely sure that you don't have any other error before? I'll add a working snippet in your question, and you'll see that the code you posted have a syntax error that I mentioned in my first comment – Calvin Nunes Feb 11 '20 at 17:02
  • ```console.log(JSON.parse(responseJson[0].data));``` this worked in parsing – Aravind Reddy Feb 11 '20 at 17:08
  • jsonlint says the (new) string is invalid – nbk Feb 11 '20 at 23:49

1 Answers1

0

Use JSON.parse('your string here'), it will convert your string to an object

Marco F
  • 19
  • 5