How to handle JSON like response in javascript?

I’m currently using jira-client to create a jira automation utility and response to getting all issues assigned to a user from that API looks something like this

{
  expand: 'schema,names',
  startAt: 0,
  maxResults: 50,
  total: 11,
  issues: [
    {
      expand: 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
      id: '3621193',
      self: 'https://<redacted>',      
      key: '<redacted>',
      fields: [Object]
    },
    {
      expand: 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
      id: '3621189',
      self: 'https://<redacted>',      
      key: '<redacted>',
      fields: [Object]
    },
    {
      expand: 'operations,versionedRepresentations,editmeta,changelog,renderedFields',
      id: '3621078',
      self: 'https://<redacted>',      
      key: '<redacted>',
      fields: [Object]
    },

I tried using JSON validators online to see whether this is valid JSON and I could see it’s not. This is of the type object in javascript when I tried the typeOf operator.

As a javascript beginner I have no idea how to extract data from this output. How can I extract data from this API response ?

This is just a JavaScript object. If you are new to JavaScript, I suggest starting with our JavaScript curriculum. If you work through this curriculum section, you should have a much better idea of how to extract the data you need from this object.

1 Like

Thank you. I just went through the basics of object handling and that solved my issue.

JSON is just an information format, commonly used with data transfer. In JS, there is no such thing as JSON, not really. You can encode JSON into a string. And JS objects are mostly compatible with JSON, as long as they have no functions, symbols, undefined. JSON also can’t handle NaN, -0, or Infinity - all things that are valid in JS. I don’t think it can handle BigInt. And JSON can’t handle a circular reference.

What is sending the API response?

If it is an API response from a trusted source it really should be sending back valid JSON. Using typeof on a JSON response is going to give you back object so that isn’t very useful.

fetch("https://jsonplaceholder.typicode.com/todos/1")
  .then((response) => response.json())
  .then((json) => {
    console.log(json); // {userId: 1, id: 1, title: 'delectus aut autem', completed: false}
    console.log(typeof json); // object
  });

There are differences between plain JS objects and JSON but when you log them out it can be hard to tell the difference.

If you want to verify it you need to look at the raw response or a stringify version and not some console log where the nested properties are not getting expanded properly (so you end up with something like property: [Object] as that won’t validate).


After understanding the fact that it’s an object, I tried accessing the data via dot operator and it worked. That solved my problem. The data comes back via the wrapper I mentioned from jira.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.