AWS json output parsing - aws-cli

I am trying to parse a json out of AWS query. I am trying to get the RedrivePolicy.deadLetterTargetArn
So, if I just do:
--output text --query "Attributes.RedrivePolicy"
Output:
{"deadLetterTargetArn":"arn:aws:sqs:us-east-2:xxxxxx:cloud-us-east-2-deadletter","maxReceiveCount":4} which is right
when I go to next level:
--output text --query "Attributes.RedrivePolicy.deadLetterTargetArn"
It says None in output.
Please find my json herein:
{
"Attributes": {
"ApproximateNumberOfMessagesNotVisible": "0",
"Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"custodian-notify-subscription\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"SQS:SendMessage\",\"Resource\":\"arn:aws:sqs:us-east-2:xxxxxxxx:cloud-us-east-2\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:us-east-2:xxxxxxxxx:cloud-us-east-2-notify\"}}}]}",
"MessageRetentionPeriod": "86400",
"ApproximateNumberOfMessagesDelayed": "0",
"RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-2:xxxxxxxxx:cloud-us-east-2-deadletter\",\"maxReceiveCount\":4}",
"MaximumMessageSize": "262144",
"CreatedTimestamp": "1561753144",
"ApproximateNumberOfMessages": "0",
"ReceiveMessageWaitTimeSeconds": "10",
"DelaySeconds": "0",
"KmsDataKeyReusePeriodSeconds": "300",
"VisibilityTimeout": "600",
"LastModifiedTimestamp": "1561753144",
"QueueArn": "arn:aws:sqs:us-east-2:xxxxxxxx:cloud-us-east-2"
}
}

Quick Answer (TL;DR)
A JMESPath query on a JSON string will not produce the expected result if you try to run a query against it using the --query keyword in AWS CLI
In the example provided, the JMESPath query does not work as expected, because the JSON element being queried is technically a JSON string (aka scalar) and not a JSON object (aka dictionary)
Detailed Answer
The key to understanding why the example is not producing the desired outcome is in distinguishing a JSON string element from a JSON object element.
We provide some examples below with an extremely simplified JSON scenario that helps illustrate what is going on here.
Example 01
In this example, we use JMESPath to query JSON. We get the expected result because we are querying against a JSON object element.
A more detailed description: We have a top-level json object with a single name-value pair.
The name in the name-value pair consists of the string "Message"
The value in the name-value pair consists of a JSON object
Digging deeper, the JSON object has two name-value pairs
// Original JSON
{
"Message": {"Greeting":"Hello","Audience":"World!"}
}
// JMESPath query
--query 'Message.Greeting'
// JMESPath result
'Hello'
Example 02
In this example, we use JMESPath to query JSON. We get an unwanted result because we are querying against a JSON string element.
A more detailed description: We have a top-level json object with a single name-value pair.
The name in the name-value pair consists of the string "Message"
The value in the name-value pair consists of a JSON string
Digging deeper, the JSON string looks similar to a JSON object, but it is not a JSON object, because it follows the conventions of a JSON string
// Original JSON
{
"Message": "{\"Greeting\":\"Hello\",\"Audience\":\"World!\"}"
}
// JMESPath query
--query 'Message.Greeting'
// JMESPath result
None (null result)
See also
JSON dot org
related JSON question on StackOverflow
related answer

Related

Value not reflecting in couchdb using putState in Hyperledger Fabric

I am trying to build a chaincode application using fabric-shim in NodeJS. When I use the putState(key,Buffer.from(value)), the value does not reflect in CouchDB, while key and other parameters (stub etc..) can be seen in CouchDB.
Any erason why it does not work?
Yes, if you look at CouchDB document, it won't show the value of a respective key. But you can see there is one attachment with a document which has actually byte array or buffer value of a key.
Please check the following image to see example attachment, if you click on it will get download as a binary file.
If you wanna fetch exact value then use the following script to get the database dump
https://github.com/danielebailo/couchdb-dump
So after database dump, you will get to see JSON object with data as one of the key in it as follows
{
"_id": <<id>>,
"_rev": <<rev>>,
"~version": <<version>>,
"_attachments": {
"valueBytes": {
"content_type": "application/octet-stream",
"revpos": 1,
"digest": <<key>>,
"data": "AAAHAQA=" ==> <<value in Base64 format>>
}
}
}
Using Base64 decoder you can decode value which will return value in form of byte array or buffer. Then you can convert byte array or buffer to respective object.

nodejs accept data from api problem with json

I am trying to get some JSON data from the API provided by the vendor, but i am getting error i try google it, in many comments people say to use JSON.Stringify but in my case JSON.Stringify didnt help me it returns an empty array like {}
repose from the api are as follow
[{ SYMBOL: 'FOREX',
CODE: 'REG',
LST: '219.50',
LTP: '12:52:35'}]
but my desire response is
[{ "SYMBOL" : "FOREX",
"CODE": "REG",
"LTP": '219.50',
"LST": '12:52:35'}]
If the JSON result provided by the vendor's API is indeed this :
[{ SYMBOL: 'FOREX',
CODE: 'REG',
LST: '219.50',
LTP: '12:52:35'}]
I have to inform you that this is invalid JSON. In JSON, the properties should be strings between double-quotes, otherwise it cannot be parsed.
Your desired response is the correct form. There is likely an error in the way the vendor is forming the output.
tl;dr : Your vendor's API is giving you a JavaScript Object, not JSON.
Your response is on text, To convert text to JSON string,
let text = `[{ SYMBOL: 'FOREX',
CODE: 'REG',
LST: '219.50',
LTP: '12:52:35'}]`
let jsonStr = JSON.stringify(eval("(" + text + ")")); // Convert Object String to JSON
console.log(jsonStr);
Note : Make sure that your vendor is trusted source, Because eval opens up your code for injection attacks, If your are worry about this, Please contact your vendor to provide JSON response.
Reference : Convert object string to JSON
Ok so what i did is use STRINGFY as a middleware and that solve my problem thanks for your help guys really appreciatable.

how to pass json object as one of the parameter in args to chaincode

How to pass json object as one of the parameters to chaincode function arguments while invoking in hyperledger fabric.
I have tried this and it comes out to be [Object, Object] with stub.getArgs() method
While calling chaincode method from NodeSDK, pass the json object as a json string using
JSON.stringify method.
When you received a json string as a parameter in chaincode, deserislize it in golang object (following is sample code)
function, args := helper.Stub.GetFunctionAndParameters()
var jsonObj interface{}
err := json.Unmarshal([]byte(args[0]), &jsonObj)
if err != nil {
fmt.Println("Can't deserialize", []byte(args[0]))
}
Note : above code will deserialize anonymous json string. If you already know your json structure then first make a same json structure by using golang struct and then deserialize your json string into object of that struct.
Should pass as a string (e.g. string containing json data). From there, you can convert to json using JSON.parse(string).
If you are using any third party Rest APIs to invoke/query the ledger , you can convert the object to base64 encoded string and decode it in chaincode.
1) Check if the JSON sent is valid.
2) You have to convert your json object to escape json object to pass as an argument.
this online tool will help in converting json object to escaped/ unescaped json.
Unescaped JSON :
[
{
"employee": {
"name": "tom",
"salary": 56000,
"married": "toJerry"
}
},
{
"employee": {
"name": "hem",
"salary": 56000,
"married": "singleForever"
}
}
]
Escaped JSON :
[{\"employee\":{\"name\":\"tom\",\"salary\":56000,\"married\":\"toJerry\"}},{\"employee\":{\"name\":\"hem\",\"salary\":56000,\"married\":\"singleForever\"}}]
which is same as JSON.stringify(object)

Replace key-value pair in an object with Google Apps Script

I am making a call to an API for a commercial product using Apps Script. Unfortunately, the resulting object has several key-value pairs that contain the id from a linked table.
I can easily get the standard values and have written code to find the related name value and add it to the object. I would prefer to add the name in the same location as the original id. But, when I add a new key, it is added to the end of the object.
I want the name in the same location as id so when I insert it into a sheet, the columns will still be in order.
This is my object:
var json = {id: 4730183,name: "A A", customer_source_id:123, company: "NE Company"};
This is my desired object after replacing the id with the name:
var json = {id: 4730183,name: "A A", source:"CRM", company: "NE Company"};
Basically, I want to find customer_source_id in the object and replace it with source.
I can't use indexOf and splice because the object is not an array.
What is the best way to do this? Do I have to convert it to an array first and then back again?
A quick answer would be:
var obj = {id: 4730183,name: "A A", customer_source_id:123, company: "NE Company"};
var json = JSON.stringify(obj);
json = json.replace("customer_source_id","source")
The better answer is:
#Waqar Ahmed is correct. JavaScript objects are unordered. In your example "var json" is an object not JSON. You can make it JSON with JSON.stringify(json). But once the JSON is parsed into an object it again becomes unordered. You should not use it to store ordered data.
I am not sure if it is efficient, but you can iterate through the keys and build a new json object like this:
var newjson = {};
for(var key in json){
if(key === 'customer_source_id'){
newjson.source = [NEW VALUE TO DISPLAY];
}else{
newjson[key] = json[key];
}
}
json = newjson;
But, like #Waqar and #Spencer said, the object is not used for ordered data.
You can do his only in java script array. Not in JSON. JSON is meant to be addressed by keys, not by index.Change your json to
var json ={id: 4730183,name: "A A", customer_source_id:null, items : [] company: "ESI"};
Now you can insert items using push method of array.
json.items.push('My Item');

getting valid JSON in Nodejs from Posted data

am getting json data as a response in the following format
{ '{"select":"samplec","value":"nation"}': '' }
How can i get a valid json data like
'{"select":"samplec", "value": "nation"}'
That is sort of an odd response to be getting, but in any case for this particular example you would do something like:
// Get the keys of your weird response object
var keys = Object.keys(response);
// The first key is a JSON string, so parse that
var obj = JSON.parse(keys[0]);
If the response had more than one key, you could loop through them all and create an array of objects. I would look into why the response was formatted the way it is, though, and see if you can't get the JSON strings delivered in some other way.

Resources