A have a big block of JSON l'm trying to parse, that looks basically like
{
"order": [
"hash1",
"hash2"
],
"posts": {
"hash4": {
"id": "hash4",
"message": "lorem ipsem"
},
"hash5": {
"id": "hash5",
"message": "dolor sit amet"
},
"hash6": {
"id": "hash6",
"message": "consectetur adipiscing elit"
}
}
}
The way I've been handling this so far is to just grep for messages
$ grep 'message' jq_dat.json
"message": "lorem ipsem"
"message": "dolor sit amet"
"message": "consectetur adipiscing elit"
This works for my current purposes, but l'd like to know how to get the same effect with jq. I.e.
$ jq .posts.<something>.message < jq_dat.json
"lorem ipsem"
"dolor sit amet"
"consectetur adipiscing elit"
I've tried using [] and {} in place of something, but those both spit back compile errors.
You just have one too many dot
jq .posts[].message < jq_dat.json
Related
I try to preprocess a JSON file and want to remove all subobjects in it.
Note that my JSON string is inline (not have \r).
In input I have:
{
"total": 2,
"result": [
{
"id":1,
"createdOn" : 123456,
"obj1": {
...:...,
...:...
},
"obj2": {
...:...,
...:...
},
"otherattribute": "..."
},
{
"id":2,
"createdOn" : 123456,
"obj1": {
...:...,
...:...
},
"obj2": {
...:...,
...:...
},
"otherattribute": "..."
}
]
}
And want to have:
{
"total": 2,
"result": [
{
"id":1,
"createdOn" : 123456,
"otherattribute": "..."
},
{
"id":2,
"createdOn" : 123456,
"otherattribute": "..."
}
]
}
I've tried to use sed command with :
sed 's/"obj1":{[^}]*//g'
It will remove the "ojb1" subobject, but let remain the endind "}," of this subobject.
I didn't find the way to also remove "},".
How?
Second question: I know the list of suboject; but is there a way to remove all theses subjects directly without knowing there name? Something like:
sed 's/".*":{[^}]*//g'
So that I will have only one sed command and not having to chain commands like :
sed 's/obj1//g' | sed 's/obj2//g' | sed 's/obj3//g' ...
I'm giving a first answer:
sed 's/"obj1":{[^}},]*//g'
It will match "obj1": until character } and then match again },, so that these last 2 chars will be also deleted (replaced with nothing).
Now I try to do something more generic to match any object name, with something like this:
sed 's/"[[:alnum:]]":{[^}},]*//g'
But it's not working...
I've already tried to escape or escape escape or escape escape escape double quote without success...
I have a my_file.json file, which contains invalid json as below, and i need to delete the lines starting from "{" upto "}," if the "name": my_script.py.
[
{
"use": abcd
"name": my_script.py
"contact": xyz
"time": 11:22:33
},
{
"use": abcd
"name": some_other_script.py
"contact": xyz
"time": 11:22:33
},
{
"use": abcd
"name": my_script.py
"contact": xyz
"time": 11:22:33
}
]
I tried below sed,
sed '//{/ {:a;/}/!{N;ba};/my_script/d}' my_file.json
but it is not working and giving me error
"sed: -e expression#1, char 11: `}' doesn't want any addresses".
This might work for you (GNU sed):
sed '/{/{:a;N;/}/!ba;/my_script\.py/d}' file
Gather up the lines between { and } and if those lines contain my_script.py delete them.
The current example is not json, however this is probably a more robust solution:
sed 's/\S\+/"&"/2;T;N;/}/!s/\n/,&/;P;D' file |
jq '[ .[]|select(.name!="my_script.py") ]'
During my searching, I would like advice about how to insert a comment in Jira issue via Talend Open Studio.
Here is my job :
So, I am trying to insert comment via Talend.
I use a tHttpRequest set like that :
uri is my string connection to get Jira account.
As it's a POST method, my header is Content-Type | application/json.
My post parameters are in a JSON file :
{
"fields": {
"project": {
"key": "TRL"
},
"summary": "A",
"description": "B",
"issuetype": {
"name": "Task"
},
"labels": ["Webapp"],
"reporter": {
"name": "x.x"
},
"assignee": {
"name": "x.x"
}
},
"body": "TEST1",
"visibility": {
"type": "role",
"value": "Administrators"
}}
When I launch this job, the following error appears :
As if the file of the response body was NULL, or maybe It's not the good manner to do the insert of the comment.
I clarify that with Insomnia(insomnia), the insertion of the comment works.
I try also the componant tRest but I don't succeed to link this one with tFileInputDelimited or tJIRAOutput.
Before to continue my work, I want to know if I am in the good direction ? Any clues ?
Thanks by advance,
Ale
I'd recommend using the tRest or tRestClient components. You can just send your JSON as "HTTP body" with these components.
On the JIRA side, you can get the necessary information here: https://developer.atlassian.com/jiradev/jira-apis
Assuming you're working with the on-premise JIRA, you'd use something like this:
URL: hostname + /rest/api/2/issue/{issueIdOrKey}/comment
HTTP Body:
{
"body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.",
"visibility": {
"type": "role",
"value": "Administrators"
}
}
Don't forget about the Authentication
I'm using the built in Web Service API in Sitefinity 9.1 which is following OData standards.
I have a URL like this to receive the JSON in the format I would like:
/api/stories/storyitems?$select=Title,Summary,ageranges,UrlName,topics,ButtonText
I have a JSON from that service that looks like this:
"value": [
{
"Title": "Developing Reading Skills",
"Summary": "Lorem ipsum dolor sit amet",
"ageranges": [
"3ca54b5b-06ab-63fa-8375-ff00000b3273"
],
"topics": [
"f0a84b5b-06ab-63fa-8375-ff00000b3273",
"efa84b5b-06ab-63fa-8375-ff00000b3273"
],
"ButtonText": "Learn More",
"UrlName": "developing-reading-skills"
}
....
]
This works great, but I am having a problem with filtering this service by ageranges or topics.
I've tried a few different solutions. This seems to be the standard OData way to filter but this throws an error. I'm not sure if this is a problem with my formatting, or the Sitefinity implementation. Can someone confirm?
/api/stories/storyitems?$filter=any(ageranges eq '3ca54b5b-06ab-63fa-8375-ff00000b3273')&$select=Title,Summary,ageranges,UrlName,topics,ButtonText
Here's the error I get back in the JSON:
{
"error": {
"code": "",
"message": "An error has occurred."
}
}
Thanks!
try:
?$filter=ageranges/any(a: a eq 'xxxx')
I was directed to this article that indicated this is the proper way:
/api/stories/storyitems?$filter=Category/any(s:s eq d81e4d00-afe6-60df-84cd-ff0000aaa3eb)&$select=Title,Summary,ageranges,UrlName,topics,ButtonText
I have a dynamically created json file.
I want to know only the "errors" part of "status". Is there any easy way to do so?
...
a lot of lines
...
"status": {
"errors": [
{
"message": "Input contained no data",
"reason": "invalid"
}
],
"state": "DONE"
}
...
a lot of lines
...
I need to use the output in a shell script so awk is preferred.
This might work for you:
sed '/^ "status": {/,/^ }$/!d;/^ "errors": \[/,/^ \],/!d' file.txt
I would use sed to select a range of lines like this:
sed -e '/^ "errors\": \[/,/^ ],/!d' file.txt
result:
"errors": [
{
"message": "Input contained no data",
"reason": "invalid"
}
],
Consider adding expected results if I have misunderstood your question.
HTH