0

I am recieving a JSON response back from an API which isnt in the right format to be parsed.

I have tried to add the missing key at the start and it won't allow it.

[
  {
    "deviceId": "9092eab10f4",
    "name": "temperature",
    "timestamp": "2017-06-13T13:19:59.673Z",
    "value": 21.5
  },
  {
    "deviceId": "9092eab10f4",
    "name": "temperature",
    "timestamp": "2017-06-13T13:19:59.673Z",
    "value": 21.5
  }
]

I would like this to have the missing key and additional curly bracket like so:

{
  "data": [
  {
    "deviceId": "9092eab10f4",
    "name": "temperature",
    "timestamp": "2017-06-13T13:19:59.673Z",
    "value": 21.5
  },
  {
    "deviceId": "9092eab10f4",
    "name": "temperature",
    "timestamp": "2017-06-13T13:19:59.673Z",
    "value": 21.5
  }
  ]
}
7
  • 1
    Why do you need the data? Being an array is valid. Commented Jul 8, 2019 at 16:12
  • You can parse the first format using JSON.parse()... Commented Jul 8, 2019 at 16:12
  • There is no such thing as a JSON Array - JSON is always a string Commented Jul 8, 2019 at 16:14
  • If you are calling the API using ajax , then just add dataType:"json" in ajax parameters , then the output of API would be automatically parsed json. Commented Jul 8, 2019 at 16:15
  • are you getting this error message - "Unexpected token [ in JSON at position #"? Commented Jul 8, 2019 at 16:31

2 Answers 2

1

I'm not sure if the response you're getting is a string or an object.

Here's a fiddle that considers both scenarios and logs your expected output to the console.

https://jsfiddle.net/6yu9ngf5/2/

I've used JSON.parse(<string>) for the case where the response is string.

For other case I just added data key to your response.

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

5 Comments

The response would be a string. A server cannot send back a JavaScript object.
@GetOffMyLawn, I agree, but the question says response is in JSON with invalid format. for an array enclosed in string JSON.parse seems like the best way to solve the issue. else need more info on the same.
So I tried using JSON.parse and it comes back with unexpected token error. Ultimately I want to get the data into an array and then process each value in a loop.
@yellamo so as shown in the fiddle, is it the same response you getting as mentioned in the part 1?
@yellamo instead of using JSON.parse(), can you try the part 2 of my fiddle, stackoverflow.com/questions/14432165/… - a similar case
0

Simple object assign?

const properResponse = Object.assign({}, {data: [response.json()]});

...assuming response is fetch, or similar with a json method which returns the response object.

2 Comments

Why not just let val = {data: await response.json()}. it's already an array. Object.assign() seems like overkill.
I think that was implied. But yes, also why i didn't use async, which may potentially confuse OP.

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.