Json parsing issue with Subarray elements in a array - json

I have a Json below which I am experiencing parse issues. I am not able to write sub array elements inside one array element. Need your help
{
"fulfillmentMessage": [{
"text": {
"text": [
"Element 1",
"Element 2",
"additionalCategory": [{
"link": "",
"followup_text": "Yes",
"title": "Yes"
},
{
"title": "No",
"followup_text": "No",
"link": ""
}
]
]
}
}]
}
For the above JSON, I am getting the below error
Error: Expecting Comma or ], not colon

You missed the curly braces before "additionalCategory".
Try this.
var json = {
"fulfillmentMessage": [{
"text": {
"text": [
"Element 1",
"Element 2",
{
"additionalCategory": [{
"link": "",
"followup_text": "Yes",
"title": "Yes"
},
{
"title": "No",
"followup_text": "No",
"link": ""
}
]
}
]
}
}]
};
console.log(json);

Related

How to get JSON data from attributes that start with # or # in Typescript

I am working with a specific API that returns a JSON that looks like the below sample.
I want to get both values that contain the #text and #attr but I get error messages in typescript when I try to get the values.
try using,
album[0]["#attr"]
album[0]["artist"]["#text"]
Hey for JSON you can use get details by its attribute name in it and it's the same for all-weather it starts with # or # it will be the same.
See below code to get the value of your specified key:
Sample JSON:
{
"weeklyalbumchart": {
"album": [
{
"artist": {
"mbid": "data",
"#text": "Flying Lotus"
},
"mbid": "data",
"url": "",
"name": "",
"#attr": {
"rank": "1"
},
"playcount": "21"
},
{
"artist": {
"mbid": "data",
"#text": "Flying Lotus"
},
"mbid": "data",
"url": "",
"name": "",
"#attr": {
"rank": "1"
},
"playcount": "21"
}
]
}
}
Read JSON:
#attr ===> json["weeklyalbumchart"]["album"][0]["#attr"]
#text ===> json["weeklyalbumchart"]["album"][0]["artist"]["#text"]
Hope this will help you to understand it.

Map conditional child elements

I am working with a JSON file which has contains lot of data that can be removed before sending to an API.
Found that JQ can be used to achieve this but not sure on how to map to get the desired results.
Input JSON
{
"name": "Sample name",
"id": "123",
"userStory": {
"id": "234",
"storyName": "Story Name",
"narrative": "Narrative",
"type": "feature"
},
"testSteps": [
{
"number": 1,
"description": "Step 1",
"level": 0,
"children": [
{
"number": 2,
"description": "Description",
"children": [
{
"number": 3,
"description": "Description"
}
]
},
{
"number": 4,
"anotherfield": "another field"
}
]
}
]
}
Desired Output
{
"name": "Sample name",
"userStory": {
"storyName": "Story Name"
},
"testSteps": [
{
"description": "Step 1",
"children": [
{
"description": "Description",
"children": [
{
"description": "Description"
}
]
},
{
"anotherfield": "anotherfield"
}
]
}
]
}
Tried to do it with the following jq command
map_values(..|{name, id, userStory})
but not sure how to filter only the userStory.storyName.
Thanks in advance.
Note: The actual JSON has different child elements that are repeated in some cases.
To delete .id from root object:
del(.id)
To leave only .storyName in .userStory:
.userStory |= {storyName}
To delete .number and .level from every object on any level in .testSteps:
.testSteps |= walk(if type == "object" then del(.number, .level) else . end)
Putting it all together:
del(.id) | (.userStory |= {storyName}) | (.testSteps |=
walk(if type == "object" then del(.number, .level) else . end))
Online demo

How to loop through a JSON object with typescript

I use angular 7.
my query return a json data which has this format :
[
{
"text": "test 1",
"value": "1",
"nbr": "1",
"children": [
{
"text": "test 1_1",
"value": "1_1",
"nbr": "2",
"children": [
{
"text": "test 1_1_1",
"value": "1_1_1",
"nbr": "1",
"children": []
},
{
"text": "test 1_1_2"",
"value": "1_1_2",
"nbr": "0",
"children": []
},
{
"text": "test 1_1_3"",
"value": "1_1_3",
"nbr": "0",
"children": []
}
]
},
{
"text": "test 1_2",
"value": "1_2",
"nbr": "0",
"children": []
}
]
},
{
"text": "test 2",
"value": "2",
"nbr": "0",
"children": []
}
]
I want to loop this data and essentially loop the children data.
and I want to make some test.
I try with this code which has problem to loop children data.
this.httpservice.query({
}).subscribe((res: HttpResponse<TestEntity[]>) => {
this.temp= res.body;
this.temp.forEach((x) => {
x["children"].forEach(x => {
if(x.nbr=='0')
{
// test code
}
x["children"].forEach(x => {
if(x.nbr=='0')
{
// test code
}
})
})
});
});
I didn't find the way to loop the children data .
Any help would be greatly appreciated
Should be something like:
const objects = Object.keys(data).map(key => data[key]);

How to Index & Search Nested Json in Solr 4.9.0

I want to index & search nested json in solr. Here is my json code
{
"id": "44444",
"headline": "testing US",
"generaltags": [
{
"type": "person",
"name": "Jayalalitha",
"relevance": "0.334",
"count": 1
},
{
"type": "person",
"name": "Kumar",
"relevance": "0.234",
"count": 1
}
],
"socialtags": {
"type": "SocialTag",
"name": "US",
"importance": 2
},
"topic": {
"type": "Topic",
"name": "US",
"score": "0.936"
}
}
When I try to Index, I'm getting the error "Error parsing JSON field value. Unexpected OBJECT_START"
When we tried to use Multivalued Field & index, we couldn't able to search using the multivalued field? Its returning "Undefined Field"
Also Please advice if I need to do any changes in schema.xml file?
You are nesting child documents within your document. You need to use the proper syntax for nested child documents in JSON:
[
{
"id": "1",
"title": "Solr adds block join support",
"content_type": "parentDocument",
"_childDocuments_": [
{
"id": "2",
"comments": "SolrCloud supports it too!"
}
]
},
{
"id": "3",
"title": "Lucene and Solr 4.5 is out",
"content_type": "parentDocument",
"_childDocuments_": [
{
"id": "4",
"comments": "Lots of new features"
}
]
}
]
Have a look at this article which describes JSON child documents and block joins.
Using the format mentioned by #qux you will face "Expected: OBJECT_START but got ARRAY_START at [16]",
"code": 400
as when JSON starting with [....] will parsed as a JSON array
{
"id": "44444",
"headline": "testing US",
"generaltags": [
{
"type": "person",
"name": "Jayalalitha",
"relevance": "0.334",
"count": 1
},
{
"type": "person",
"name": "Kumar",
"relevance": "0.234",
"count": 1
}
],
"socialtags": {
"type": "SocialTag",
"name": "US",
"importance": 2
},
"topic": {
"type": "Topic",
"name": "US",
"score": "0.936"
}
}
The above format is correct.
Regarding searching. Kindly use the index to search for the elements of the JSON array.
The workaround for this can be keeping the whole JSON object inside other JSON object and the indexing it
I was suggesting to keep the whole data inside another JSON object. You can try the following way
{
"data": [
{
"id": "44444",
"headline": "testing US",
"generaltags": [
{
"type": "person",
"name": "Jayalalitha",
"relevance": "0.334",
"count": 1
},
{
"type": "person",
"name": "Kumar",
"relevance": "0.234",
"count": 1
}
],
"socialtags": {
"type": "SocialTag",
"name": "US",
"importance": 2
},
"topic": {
"type": "Topic",
"name": "US",
"score": "0.936"
}
}
]
}
see the syntax in http://yonik.com/solr-nested-objects/
$ curl http://localhost:8983/solr/demo/update?commitWithin=3000 -d '
[
{id : book1, type_s:book, title_t : "The Way of Kings", author_s : "Brandon Sanderson",
cat_s:fantasy, pubyear_i:2010, publisher_s:Tor,
_childDocuments_ : [
{ id: book1_c1, type_s:review, review_dt:"2015-01-03T14:30:00Z",
stars_i:5, author_s:yonik,
comment_t:"A great start to what looks like an epic series!"
}
,
{ id: book1_c2, type_s:review, review_dt:"2014-03-15T12:00:00Z",
stars_i:3, author_s:dan,
comment_t:"This book was too long."
}
]
}
]'
supported from solr 5.3

json parsererror

why this error
xhr: [object XMLHttpRequest]
textStatus: parsererror
errorThrown: Invalid JSON: [ { "id": "10000000", "label": "10000000" }, { "id": "10000001", "label": "10000001" }, { "id": "10000003", "label": "10000003" }, { "id": "10000005", "label": "10000005" }, { "id": "10000006", "label": "10000006" } ]
Copied and pasted this:
[ { "id": "10000000", "label": "10000000" }, { "id": "10000001", "label": "10000001" }, { "id": "10000003", "label": "10000003" }, { "id": "10000005", "label": "10000005" }, { "id": "10000006", "label": "10000006" }]
to JSONlint and it says it's valid.
Can you post the original JSON if that is the original then the it is missing a root key
var textStatus = {"keys":[{"id":"...", "label":"..."},{"id":"...","label":"..."}]}
Also the whole thing needs to be inside {} to make it an object what you had is just an array