Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I cant get it done, need some community brainpower here. Please find the source data and the desired results below. I am currently familiarizing myself with JSONata.
I have tried almost all combinations in the JSONata docs, but I am getting strange results back.
Source data
[
{
"id": 784521,
"name": "T-Shirt - Red / 60 XXL",
"properties": [
{
"name": "customization",
"value": "Text, Logo"
},
{
"name": "customization_text",
"value": "John Doe"
},
{
"name": "customization_text_pos",
"value": "Left"
},
{
"name": "customization_logo",
"value": "https://https://picsum.photos/200/300"
},
{
"name": "customization_logo_pos",
"value": "Right"
}
],
"quantity": 8,
"sku": "888-111"
},{
"id": 154857,
"name": "Pullover - Blue / 48 L",
"properties": [
{
"name": "customization",
"value": "Text"
},
{
"name": "customization_text",
"value": "John Doe"
},
{
"name": "customization_text_pos",
"value": "Right"
}
],
"quantity": 4,
"sku": "555-111"
}
]
Desired result
{
"products": [
{
"name": "T-Shirt - Red / 60 XXL",
"quantity": 8,
"customization": "Text, Logo",
"customization_text": "John Doe",
"customization_text_pos": "Left",
"customization_logo": "https://https://picsum.photos/200/300",
"customization_logo_pos": "Right"
},
{
"name": "Pullover - Blue / 48 L",
"quantity": 4,
"customization": "Text",
"customization_text": "John Doe",
"customization_text_pos": "Right",
"customization_logo": "",
"customization_logo_pos": ""
}
]
}
I'm not sure how important for your case to populate the missing properties with an empty string value, but if not - reducing your properties array into an object lets you achieve the result:
{
"products": $$.(
$item := $;
properties{
"name": $item.name,
"quantity": $item.quantity,
name: value
}
)
}
See it live: https://stedi.link/6htarTz
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I need to transform a JSON reponse from :
[
{
"value": {
"name": "Actions",
"visible": true,
"property": "actions"
}
},
{
"value": {
"name": "Checkbox",
"visible": true,
"property": "checkbox"
}
}
]
to :
[
{
"name": "Actions",
"visible": true,
"property": "actions"
},
{
"name": "Checkbox",
"visible": true,
"property": "checkbox"
}
]
What is the "best" way to achieve this and remove the key "value" ?
Is it a foreach loop or are the other ways ? Trying to learn new ways as well
Best, Peter
You could use the build in map function to achieve this:
let array = [
{
"value": {
"name": "Actions",
"visible": true,
"property": "actions"
}
},
{
"value": {
"name": "Checkbox",
"visible": true,
"property": "checkbox"
}
}
]
let mapped_aray = array.map(x => x.value);
The problem is: I have 2 cucumber test reports in JSON format
I need to remove redundant key-value pairs from those reports and compare them, but I can't understand how to remove the unnecessary data from those 2 jsons because of their structure after JSON.parse (array or hash with many nested arrays/hashes). Please advice if there are some gems or known solutions to do this
JSON structure is e.g. :
[
{
"uri": "features/home_screen.feature",
"id": "as-a-user-i-want-to-explore-home-screen",
"keyword": "Feature",
"name": "As a user I want to explore home screen",
"description": "",
"line": 2,
"tags": [
{
"name": "#home_screen",
"line": 1
}
],
"elements": [
{
"keyword": "Background",
"name": "",
"description": "",
"line": 3,
"type": "background",
"before": [
{
"match": {
"location": "features/step_definitions/support/hooks.rb:1"
},
"result": {
"status": "passed",
"duration": 505329000
}
}
],
"steps": [
{
"keyword": "Given ",
"name": "I click OK button in popup",
"line": 4,
"match": {
"location": "features/step_definitions/registration_steps.rb:91"
},
"result": {
"status": "passed",
"duration": 2329140000
}
},
{
"keyword": "And ",
"name": "I click Allow button in popup",
"line": 5,
"match": {
"location": "features/step_definitions/registration_steps.rb:96"
},
"result": {
"status": "passed",
"duration": 1861776000
}
}
]
},
Since you are asking for a gem, you might try iteraptor I have created exactly for this kind of tasks.
It allows iterating, mapping and reducing the deeply nested structures. For instance, to filter out all the keys called "name" on all levels, you might do:
input.iteraptor.reject(/name/)
The more detailed description might be found on the github page linked above.
This question already has answers here:
How to deserialize JSON with duplicate property names in the same object
(3 answers)
Closed 6 years ago.
I'm trying to parse a JSON string to a JObject, but somehow it only parses the first object of an Array.
This is a part of a JSON string
{
"Categories": [
{
"Category": [
{
"ID": "1",
"Description": "Kochen/Backen",
"IsActive": "True"
}
],
"Category":[
{
"ID": "2",
"Description": "Sport",
"IsActive": "True"
}
],
"Category": [
{
"ID": "3",
"Description": "Begleitung 2",
"IsActive": "True"
}
]
}
],
And after JObject.Parse, I can see it loaded:
{
"Categories": [
{
"Category": [
{
"ID": "3",
"Description": "Begleitung 2",
"IsActive": "True"
}
]
}
],
So why are the first 2 Categories not parsed?
I'm not a pro with JSON, so I don't know if the string is correct that way.
Thanks for your help
Your JObject cannot hold duplicate keys. The dictionary in the parent list, has multiple entries with same key Category:
{
"Category": [
{
"ID": "1",
"Description": "Kochen/Backen",
"IsActive": "True"
}
],
"Category":[
{
"ID": "2",
"Description": "Sport",
"IsActive": "True"
}
],
"Category": [
{
"ID": "3",
"Description": "Begleitung 2",
"IsActive": "True"
}
]
}
So the other keys are overwritten after parsing and the last item with id 3 becomes the final value. Consider restructuring the keys into say Category1, Category2 and Category3
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a string of JSON formatted data and I'm simply trying to save this as a dataframe. Here's my JSON data:
newobject <-
{
"data": [
{
"links": [
{
"rel": "self",
"href": "https://api.nike.com/v1/me/sport/activities/8102000000014097347280007666912117966998"
}
],
"activityId": "8102000000014097347280007666912117966998",
"activityType": "RUN",
"startTime": "2015-07-25T13:27:52Z",
"activityTimeZone": "America/Anguilla",
"status": "COMPLETE",
"deviceType": "SPORTWATCH",
"metricSummary": {
"calories": "1012",
"fuel": "3338",
"distance": "13.059499740600586",
"steps": "0",
"duration": "1:05:27.000"
},
"tags": [],
"metrics": []
}
],
"paging": {
"next": "/v1/me/sport/activities/RUNNING?count=1&access_token=Qr7kEcwD6bpfxy2mJ1yH8uGBGfdY&offset=2",
"previous": null
}
}
)
When I try to save this to an object in R, I get an error:
ERROR: unexpected '}' in "}"
First you have to define your object as a string in R, just use simple quotes...
newobject <-
'{
"data": [
{
"links": [
{
"rel": "self",
"href": "https://api.nike.com/v1/me/sport/activities/8102000000014097347280007666912117966998"
}
],
"activityId": "8102000000014097347280007666912117966998",
"activityType": "RUN",
"startTime": "2015-07-25T13:27:52Z",
"activityTimeZone": "America/Anguilla",
"status": "COMPLETE",
"deviceType": "SPORTWATCH",
"metricSummary": {
"calories": "1012",
"fuel": "3338",
"distance": "13.059499740600586",
"steps": "0",
"duration": "1:05:27.000"
},
"tags": [],
"metrics": []
}
],
"paging": {
"next": "/v1/me/sport/activities/RUNNING?count=1&access_token=Qr7kEcwD6bpfxy2mJ1yH8uGBGfdY&offset=2",
"previous": null
}
}'
After that, you can convert the string into a data frame using the jsonlite package (install.packages("jsonlite")).
library(jsonlite)
df <- fromJSON(newobject)
Then if you run df$data you get your data.frame.
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