Can we change the sequence of json items?
For example:
[
{
"category": "Science: Mathematics",
"type": "multiple",
"difficulty": "medium",
"question": "In a complete graph G, which has 12 vertices, how many edges are there?",
"correct_answer": "66",
"incorrect_answers1": "67",
"incorrect_answers2 : "34",
"incorrect_answers3 : "11"
},
{
"category": "Science: Mathematics",
"type": "multiple",
"difficulty": "medium",
"question": "In base 2, what is 1 + 1?",
"incorrect_answers1": "2",
"incorrect_answers2 : "01",
"correct_answer": "10",
"incorrect_answers3 : "11"
},
{
"category": "Science: Mathematics",
"type": "multiple",
"difficulty": "medium",
"question": "In the hexadecimal system, what number comes after 9?",
"incorrect_answers1": "10",
"incorrect_answers2 : "The Number 0",
"correct_answer": "The Letter A",
"incorrect_answers3 : "16"
}
]
As mentioned in the comments that re-ordering JSON data is impossible and also not required.
For the current situation, you have to loop across the parent array containing the JSON objects, create a local array variable to get the list of re-ordered answers, push all the answers to this array and shuffle it using a library like lodash (https://lodash.com/docs/#shuffle) or a custom random functions, whatever works for your needs.
You can place this array within each object of this JSON while looping and give it a meaningful key like shuffled_answers.
So now your item structure will look like this:
{
"category": "Science: Mathematics",
"type": "multiple",
"difficulty": "medium",
"question": "In base 2, what is 1 + 1?",
"incorrect_answers1": "2",
"incorrect_answers2" : "01",
"correct_answer": "10",
"incorrect_answers3" : "11",
"shuffled_answers": ["2", "11", "10", "11"]
}
You can then use this array to display answer choices in your UI to ask user the question and use the correct_answer key to compare the correct answer from the user's choice.
I'm linking here a video tutorial for a very similar App developed in Vue using the above logic. You can refer for more insight about apps like these and use it for your requirements.
Hope that helps.
Related
I am transforming JSON objects and index them in Solr 9. When resolving lists / arrays of objects, I am using nested child documents, so array elements are stored as own documents.
Now, I ran into the issue that I would like to use copy fields on nested child documents and store this value in the parent.
JSON:
{
"legalName": "Some Name",
"addresss": {
"street": "Bala Street",
"houseNr": 13,
"city": "Random City",
"postalCode": 1234,
"country": "NL"
},
"otherLegalNames": [
{
"text": "TEXT IN EN",
"lang": "EN"
},
{
"text": "TEXT IN DE",
"lang": "DE"
},
{
"text": "TEXT IN NL",
"lang": "NL"
}
]
}
When indexing this object, I flatten basic structs like address but keep arrays, e.g., otherLegalNames, and stored them through child docs.
Basically, the documents look like this (q=*:*&fl=*,[child]):
{
"id": "5493006O42CR4SHELP26",
"legalName": "Some Name",
"addresss.street": "Bala Street",
"addresss.houseNr": 13,
"addresss.city": "Random City",
"addresss.postalCode": 1234,
"addresss.country": "NL",
"otherLegalNames": [
{
"id": "5493006O42CR4SHELP26/otherLegalNames#0",
"text": "TEXT IN EN",
"lang": "EN"
},
{
"id": "5493006O42CR4SHELP26/otherLegalNames#1",
"text": "TEXT IN DE",
"lang": "DE"
},
{
"id": "5493006O42CR4SHELP26/otherLegalNames#2",
"text": "TEXT IN NL",
"lang": "NL"
}
]
}
Now I would like to search for these docs by their legalName and must therefore search in the parent legalName field but also include all text fields stored under otherLegalNames. During research, I found that copy fields are the way to go, but I am not sure how I would handle child documents with such copy fields.
My goal would be to get a searchableLegalNames field with the value: ["Some Name", "TEXT IN EN", "TEXT IN DE", "TEXT IN NL"] or similar to perform a Ngram based search on legalName including every language.
Is this possible to achieve with copy fields or are child documents not supported for this purpose? If so, how should I restructure my schema? It's really hard to flatten every legal name, as this array might be empty or contain an arbitrary number of otherLegalNames.
Thanks.
Regards Artur
This question already has an answer here:
Select multiple fields at different levels
(1 answer)
Closed 2 years ago.
I have a file, let's call it heroes.json, where part of the data is a nested array of object, superpowers:
[
{
"hero": "Superman",
"id": "123",
"realName": "Clark Kent",
"age": "?",
"superpowers": [
{
"name": "speed",
"num": "1",
"des": "Faster than a speeding bullet.",
"value": "50"
},
{
"name": "strength",
"num": "2",
"des": "More powerful than a locomotive.",
"value": "100"
}
],
"weakness": "kryptonite"
},
{
"hero": "Batman",
"id": "456",
...
I want to select hero and superpowers, and keep only name and des keys within superpowers, like:
[
{
"hero": "Superman",
"superpowers": [
{
"name": "speed",
"des": "Faster than a speeding bullet."
},
{
"name": "strength",
"des": "More powerful than a locomotive."
}
]
},
{
"hero": "Batman",
"superpowers": [
...
It wouldn't be hard to write an iterator to do this, but I want to try jq as I'm new to this tool and it seems useful to learn.
So I experimented on jqplay until it delivered the needed format. I don't know if it's optimal, but this worked:
jq '[.[] | {hero, superpowers: [ .superpowers[] | {name, des} ] } ]'
(A graphQL-like filter syntax would make this easier.)
Note: The output required for my json differs from what is mentioned in this question and answer, and I have avoided using map (iterator) in the solution. In other words, I'm not asking the same question, or presenting the same answer.
It would be helpful to know if my solution is optimal.
I have the following HTTP Post with the following payload
{
"externalCode": "999",
"name": "PNNL - Winthrop, WA (Sundown L",
"description": "Winthrop",
"geozoneFlx": "PNL",
"status": "A",
"address1": "135 Sundown Lane",
"city": "WINTHROP",
"state": "WA",
"county": "OKANOGAN",
"country": "USA",
"zipCode": "98862-0339",
"timezone": "PST",
"startDate": "2016-01-27T00:00:00",
"endDate": "9999-12-31T00:00:00"
},
HOWEVER, some data message come into the Logic App looking like this with no "State" "name" in the data like this
"externalCode": "999",
"name": "PNNL - Winthrop, WA (Sundown L",
"description": "Winthrop",
"geozoneFlx": "PNL",
"status": "A",
"address1": "135 Sundown Lane",
"city": "WINTHROP",
"county": "OKANOGAN",
"country": "USA",
"zipCode": "98862-0339",
"timezone": "PST",
"startDate": "2016-01-27T00:00:00",
"endDate": "9999-12-31T00:00:00"
},
I have to say if the data message comes in without the State json Name like above, kick it out. How do I filter for a missing json field such as "State" in the above??
Thanks
Mike
I think you can judge whether the length of the value of the state field is greater than 0, if it is greater than 0, execute your original logic, if the length of the value is 0, you don't need to do anything. The design of the logic app is as follows:
If the request body of your http request is a json array, you can use the for each loop to process.
The expression for obtaining the length of the state field in condition is as follows:
length(coalesce(body('Parse_JSON')?['state'], ''))
For the usage of coalesce and length, you can refer to the official documentation
I'm doing a Http get in Angular 2 and the response is a JSON. However, i'm trying to use this in a ngFor but i can't because it isn't an Array.
How can I convert JSON to Array in Angular 2? I searched in many websites but didn't discover a effective way to do that.
Edit 1:
The response is like that:
{
"adult": false,
"backdrop_path": "/fCayJrkfRaCRCTh8GqN30f8oyQF.jpg",
"belongs_to_collection": null,
"budget": 63000000,
"genres": [
{
"id": 18,
"name": "Drama"
}
],
"homepage": "",
"id": 550,
"imdb_id": "tt0137523",
"original_language": "en",
"original_title": "Fight Club",
"overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
"popularity": 0.5,
"poster_path": null,
"production_companies": [
{
"name": "20th Century Fox",
"id": 25
}
],
"production_countries": [
{
"iso_3166_1": "US",
"name": "United States of America"
}
],
"release_date": "1999-10-12",
"revenue": 100853753,
"runtime": 139,
"spoken_languages": [
{
"iso_639_1": "en",
"name": "English"
}
],
"status": "Released",
"tagline": "How much can you know about yourself if you've never been in a fight?",
"title": "Fight Club",
"video": false,
"vote_average": 7.8,
"vote_count": 3439
}
I think if you want to pass from json to array you could do the following command:
var arr = []
for(i in json_object){
arr.push(i)
arr.push(json_object[i])
}
Then you have every keys in the even index and every contents in the odd index
Well, I really don't see the point here. Arrays are for operating with lists of similar objects or types, not for complex structures. If you had a bunch of objects similar to the one you show, then it would make sense. Anyways, if you really want an array then you could do it with recursion and create a flat array of the properties.
var flatPropertyArray = [];
function flatten(obj) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object")
flatten(obj[property]);
else
flatPropertyArray.push(property);
}
}
}
pass your JSON into the flatten func.
I've been looking at some StackOverflow cases such as this case, but I cannot find an example with a document structure close to this one.
Below is an example of one document within my collection artistTags. All documents follow the same structure.
{
"_id": ObjectId("5500aaeaa7ef65c7460fa3d9"),
"toptags": {
"tag": [
{
"count": "100",
"name": "Hip-Hop"
},
{
"count": "97",
"name": "french rap"
},
...{
"count": "0",
"name": "seen live"
}
],
"#attr": {
"artist": "113"
}
}
}
1) How can I find() this document using the "artist" value (here "113")?
2) How can I retrieve all "artist" values having a specific "name" value (say "french rap") ?
Referring to chridam answer here above:
db.collection.find({"toptags.#attr.artist": "113"})