0

How are we going to parse following type of object in angular or javascript, maybe using for search loop or parsing?

I wanted to get the title value and assign it to title because as you can see the value of title is object:

{'title': 'Hey', 'instruction': 'Take a sad song a…75, 'sub_title': 'Jude', 'timelimit': '01:05:01'}

instead of "Hey" as you can see on the example (same also with the second object). Is there a way we can do that?

JSON array of objects format:

[  
   {  
      id:0,
      title:"{'title': 'Hey', 'instruction': 'Take a sad song a…75, 'sub_title': 'Jude', 'timelimit': '01:05:01'}"
   },
   {  
      id:1,
      title:"{'title': 'Assessment', 'instruction': 'Jude', 'cr…71, 'sub_title': 'Test', 'timelimit': '06:25:08'}"
   }
]

Desired output:

[  
   {  
      id:0,
      title:"Hey"
   },
   {  
      id:1,
      title:"Assessment"
   }
]
5
  • 1
    Its not very clear what you are trying to do, can you explain it any better? In you first JSON snippet, title is a string not an object, in the second snippet an array of two elements, title is an object, what are you trying to do? Commented Mar 6, 2019 at 7:15
  • please explain what do you want to achieve Commented Mar 6, 2019 at 7:16
  • i have update my question Commented Mar 6, 2019 at 7:17
  • your title value does not properly stringify.... check your title Commented Mar 6, 2019 at 7:20
  • is there a solution to that using that format>? Commented Mar 6, 2019 at 7:21

4 Answers 4

1

Make sure your have the correct format in json - double quotation marks inside and single outside.

Like this

'{"title": "Hey", "instruction": "Take a sad song a…75", "sub_title": "Jude", "timelimit": "01:05:01"}'

Then you can simply do.

let jsonString = '{"title": "Hey", "instruction": "Take a sad song a…75", "sub_title": "Jude", "timelimit": "01:05:01"}';
let title = JSON.parse(jsonString).title;
console.log(title);

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

4 Comments

the example above is already the response i got , i can changed the format.
The second example 'instruction': 'Jude', 'cr…71, 'sub_title' is invalid json for sure, can confirm this is what you get as a response?
I got that data from an api , thats the reponse data
I guess you copied directly as is shown in the developer tools. The problem is it won't show full response there as the data might be too long and so when you copied, you copied only partial response and that too invalid format.
1

var jsonObj = [  
   {  
      id:0,
      title:"{'title': 'Hey', 'instruction': 'Take a sad song a…75, 'sub_title': 'Jude', 'timelimit': '01:05:01'}"
   },
   {  
      id:1,
      title:"{'title': 'Assessment', 'instruction': 'Jude', 'cr…71, 'sub_title': 'Test', 'timelimit': '06:25:08'}"
   }
];


var updatedJsonObj = jsonObj.map( obj => { 
  return {
      ...obj, 
      title: JSON.parse(obj.title).title
  } 
});

console.log(updatedJsonObj);

//updatedJsonObj will have your required format

6 Comments

@JhonCaylog thanks for mentioning, corrected the answer
Unexpected token ' in JSON at position 1
@JhonCaylog JSON you provided has issues do you have actual json correct one? for example: "{'title': 'Hey', 'instruction': 'Take a sad song a…75, 'sub_title': 'Jude', 'timelimit': '01:05:01'}" has missing single quote and the other one as well completely wrong json, unless you give correct json no solution can work on that data set. Please correct the json object
that is the response data i got from an api bro
is there a way we can cover up missing single quote and make it a correct json? without doing it manually
|
0

Here we are doing following steps

  1. Iterate over array
  2. For each object, title field is not valid JSON. make it valid by using title.replace(/'/g, '"'). Then Parse JSON.
  3. Then assign title of parsed JSON to title of object

Here is the code

arr = [  
   {  
      id:0,
      title:"{'title': 'Hey', 'instruction': 'Take a sad song a…75', 'sub_title': 'Jude', 'timelimit': '01:05:01'}"
   },
   {  
      id:1,
      title:"{'title': 'Assessment', 'instruction': 'Jude', 'sub_title': 'Test', 'timelimit': '06:25:08'}"
   }
]

arr =  arr.map((e)=> { e.title = JSON.parse(e.title.replace(/'/g, '"')).title; return e; })
// expected result is in arr

.

4 Comments

Unexpected token t in JSON at position 117
Its working as expected in browser console, you can try it in console
Hi bro i have a question
What is the question? please ask
0

As others have stated your json is not valid but since you have mentioned that it is what you get from your backend and you cannot change it, I suggest treat your title as string and use string operations to get the desired value.

for example you can use the following to get ID and title

<div ng-repeat="item in data">
  {{item.id}} - 
  {{item.title.split('\'')[3]}}
</div>    

Demo

Comments

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.