Remove special characters from a specific JSON object in JSON payload - javascript

I have a large JSON payload and I want to format, the specific object of the payload using JS.
I want flightdetails array object to edit and remove the special characters from it. How can I achieve this?
I have been working on this using XSLT for the past 2 days, and I went nowhere hence I decided to remove it using JS.
Example of the array(there can be more than 30 records inside the flightdetails)
"flightdetails": [
{
"id": XF-2092,
"trips": 2,
"categories": {
"flights": [
"\"return\",\"oneway\""
]
},
"id": XF-2093,
"trips": 1,
"categories": {
"flights": [
"\"return\""
]
}
}
]
Expected Output
"flightdetails": [
{
"id": XF-2092,
"trips": 2,
"categories": {
"flights": [
"return","oneway"
]
},
"id": XF-2093,
"trips": 1,
"categories": {
"flights": [
"return"
]
}
}
]
The flightdetails object inside the //destinations/flightdetails path

Related

How to combine two JSON objects into an array

I have two JSON files - one pertaining to English Premier League data of 2015-16 and other of 2016-17. The JSON data in each file is as follows (extract only):
{
"name": "English Premier League 2015/16",
"rounds": [
{
"name": "Matchday 1",
"matches": [
{
"date": "2015-08-08",
"team1": {
"key": "manutd",
"name": "Manchester United",
"code": "MUN"
},
"team2": {
"key": "tottenham",
"name": "Tottenham Hotspur",
"code": "TOT"
},
"score1": 1,
"score2": 0
},
and so on... lot of name, matches like this in the rounds array.
The data of 2016-17 is similar to above:
{
"name": "English Premier League 2016/17",
"rounds": [
{
"name": "Matchday 1",
"matches": [
{
"date": "2016-08-13",
"team1": {
"key": "hull",
"name": "Hull City",
"code": "HUL"
},
"team2": {
"key": "leicester",
"name": "Leicester City",
"code": "LEI"
},
"score1": 2,
"score2": 1
},
and so on... lot of name, matches like this in the rounds array.
These two are in two separate json files. I am developing an application in Angular 2 and have used a data service to get this data. That part was successful. However I need to use ngFor for creating rows in table and for that I require this data to be iterable like an array.
I have tried to convert these two json objects into an array but could not succeed.
1 I have initialised an empty array and tried to do array.push for each object. That did not work
arr=[];
if(this.data1516!=null && this.data1516!=undefined) {arr.push(this.data1516);}
if(this.data1617!=null && this.data1617!=undefined) {arr.push(this.data1617);}
2 I tried Object. assign -
this.obj=Object.assign({},this.data1516,this.data1617);
and then tried converting the object into array, even that did not work
All I want the final json single file to be an array of those two objects, like this:
[ {obj1} , {obj2} ]
There was a extra ] inside arr.push
This should work :
arr=[];
if(this.data1516!=null && this.data1516!=undefined) {
arr.push(this.data1516);
}
if(this.data1617!=null && this.data1617!=undefined) {
arr.push(this.data1617);
}
OR Shorter version (ES6)
arr = [ ...this.data1516 , ...this.data1617 ];

how to access nested values inside json object inside javascript [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 5 years ago.
I have this URL which returns me a JSON value, my task is to access 3 values out of it that is name, title and ISBN.
Since the content is heavily nested, I am not able to access the values inside it.
All the tutorials I have referred so far, have simple nesting in them
I have attached my code that I have written so far:
$(document).ready(function() {
var globalJsonVar;
alert("check");
$.getJSON("https://openlibrary.org/api/books?bibkeys=ISBN:9781606868829&jscmd=details&format=json", function(result) {
if (Object.keys(result).length > 0) {
alert("conetnt is present");
console.log(result);
alert(Object.values(result));
alert(result.ISBN: 9781606868829. bib_key); // my attempt at accessing the values
} else {
alert("hoax");
}
});
});
The JSON content that I am receiving is
{
"ISBN:9780143039648": {
"bib_key": "ISBN:9780143039648",
"preview": "noview",
"preview_url": "https://openlibrary.org/books/OL17924003M/The_guide",
"info_url": "https://openlibrary.org/books/OL17924003M/The_guide",
"details": {
"number_of_pages": 196,
"series": [
"Penguin classics"
],
"lc_classifications": [
"PR9499.3.N3 G85 2006"
],
"latest_revision": 7,
"uri_descriptions": [
"Contributor biographical information",
"Publisher description"
],
"genres": [
"Fiction."
],
"title": "The guide",
"languages": [{
"key": "/languages/eng"
}],
"subjects": [
"Malgudi (India : Imaginary place) -- Fiction.",
"Tour guides (Persons) -- Fiction.",
"Bharata natyam dancers -- Fiction.",
"Spiritual life -- Hinduism -- Fiction.",
"India -- Fiction."
],
"publish_country": "nyu",
"by_statement": "R.K. Narayan ; introduction by Michael Gorra.",
"type": {
"key": "/type/edition"
},
"uris": [
"http://www.loc.gov/catdir/enhancements/fy0716/2006044314-b.html",
"http://www.loc.gov/catdir/enhancements/fy0716/2006044314-d.html"
],
"revision": 7,
"publishers": [
"Penguin Books"
],
"dewey_decimal_class": [
"823/.914"
],
"last_modified": {
"type": "/type/datetime",
"value": "2012-08-05T21:10:58.887826"
},
"key": "/books/OL17924003M",
"authors": [{
"name": "Rasipuram Krishnaswamy Narayan",
"key": "/authors/OL5911201A"
}],
"publish_places": [
"New York"
],
"oclc_number": [
"65644730"
],
"pagination": "xxiv, 196 p. ;",
"created": {
"type": "/type/datetime",
"value": "2008-10-07T19:30:19.584308"
},
"url": [
"http://www.loc.gov/catdir/enhancements/fy0716/2006044314-b.html",
"http://www.loc.gov/catdir/enhancements/fy0716/2006044314-d.html"
],
"notes": {
"type": "/type/text",
"value": "Originally published: New York : Viking Press, 1958.\n\nIncludes bibliographical references (p. [xxi]-xxii)."
},
"identifiers": {
"goodreads": [
"129877"
],
"librarything": [
"6930"
]
},
"isbn_13": [
"9780143039648"
],
"lccn": [
"2006044314"
],
"isbn_10": [
"0143039644"
],
"publish_date": "2006",
"works": [{
"key": "/works/OL1057183W"
}]
}
}
}
You have to use [] if you want to have access to keys that have special chars, for example result['something-with:special-chars']
result.ISBN:9781606868829.bib_key
is a syntax error. : is not allowed here. You want to use the following syntay :
result["ISBN:9781606868829"].bib_key

Stringify JSON differently on each level

In order to hash (sha1) a JSON and to compare the resulting hash to a given one, I need a String which is exactly the same as the one that has been used to create the given hash.
Then, my stringified JSON must have different indentation for sub nodes of the JSON :
I want this js object :
{id:4,name:'patrick',roles:[{id:1,name:'admin'},{id:2,name:'author'}]}
to be stringified like that :
"{
"id": "4",
"name": "patrick",
"roles": [
{"id":1,"name":"admin"},
{"id":2,"name":"author"}
]
}"
JSON.stringify(obj,null,4) gives me something very near from the solution but not exactly :
"{
"id": 4,
"name": "patrick",
"roles": [
{
"id": 1,
"name": "admin"
},
{
"id": 2,
"name": "author"
}
]
}"
I'm trying with the second argument of JSON.stringify but can not reach my goal :'(

Can't get json data inside array

[
{
"May": [
{
"year": 1994,
"date": "2"
},
{
"Sequence": 2,
"Type": "Images"
}
],
"_id": "1122"
}
]
I can get id except "date" which is inside array. And date just show me undefined.
var data = [
{
"May": [
{
"year": 1994,
"date": "2"
},
{
"Sequence": 2,
"Type": "Images"
}
],
"_id": "1122"
}
];
alert(data[0].May[0]["date"]);
You should use arr[i].May[0]['date'], date is inside the object of the 0th index of array May.
Array will be start with the index 0 only . so from your json data 0th position only have the key named "date". So you should use like #void answer.
If you had doubt about whether data is have or not use like below
if(data[0].May[0]["date"])
alert(data[0].May[0]["date"]);
Thank you :)

Searching and editing nested JSON elements

Basically I want to be able, in Javascript (JQuery optionally), to search into a JSON with nested elements for a particular element and edit it.
Ex. search for "components" with id 110 and change the name to "video card".
Notice the following JSON is just an example. I am wondering if javascript libraries or good tricks exist to do such a thing, I don't think traversing the whole json or writing my own methods is the best solution.
{
"computers": [
{
"id": 10,
"components": [
{
"id": 56,
"name": "processor"
},
{
"id": 24,
"name": "ram"
}
]
},
{
"id": 11,
"components": [
{
"id": 110,
"name": "graphic card"
},
{
"id": 322,
"name": "motherboard"
}
]
}
]
}
You could try linq.js.
You can use this javascript lib, DefiantJS (http://defiantjs.com), with which you can filter matches using XPath on JSON structures. To put it in JS code:
var data = {
"computers": [
{
"id": 10,
"components": [
{ "id": 56, "name": "processor" },
{ "id": 24, "name": "ram" }
]
},
{
"id": 11,
"components": [
{ "id": 110, "name": "graphic card" },
{ "id": 322, "name": "motherboard" }
]
}
]
},
res = JSON.search( data, '//components[id=110]' );
res[0].name = 'video card';
Here is a working fiddle;
http://jsfiddle.net/g8fZw/
DefiantJS extends the global object JSON with the method "search" and returns an array with matches (empty array if no matches were found). You can try out the lib and XPath queries using the XPath Evaluator here:
http://www.defiantjs.com/#xpath_evaluator

Categories

Resources