I have a simple JSON file in AWS that has a very simple array in the format below
{
"student_ids": [5466,232,32145]
}
I am trying to reading the id's and place them in an array in ruby. I have access to the file and can access the data. I just can't figure out how to parse it into my ruby array
you can use the following
require 'json'
hash = JSON.parse('{"student_ids": [1,2,3,3] }')
p hash["student_ids"]
Related
I want to write a function that takes a json array as input parameter and then to parse the array and store each json element in a text variable to use later.
I am using Postgres 9.3 and using json_array_elements() to help but cant go further.
I have a json and value out of json
000000,{"000":{"phoneNumber":null,"firstName":"xyz","lastName":"pqr","email":"email#xyz.com","alternatePickup":true,"sendTextNotification":false,"isSendTextNotification":false,"isAlternatePickup":true}}
I'm trying to load this json in pig using elephant bird json loader but unable to do that.
I'm able to load the following json
{"000":{"phoneNumber":null,"firstName":"xyz","lastName":"pqr","email":"email#xyz.com","alternatePickup":true,"sendTextNotification":false,"isSendTextNotification":false,"isAlternatePickup":true}}
Using following script -
REGISTER json-simple-1.1.1.jar;
REGISTER elephant-bird-pig-4.3.jar;
REGISTER elephant-bird-hadoop-compat-4.3.jar;
json_data = load 'ek.json' using com.twitter.elephantbird.pig.load.JsonLoader() AS (json_key: [(phoneNumber:chararray,firstName:chararray,lastName:chararray,email:chararray,alternatePickup:boolean,sendTextNotification:boolean,isSendTextNotification:boolean,isAlternatePickup:boolean)]);
dump json_data;
But when I include value out of json
json_data = load 'ek.json' using com.twitter.elephantbird.pig.load.JsonLoader() AS (id:int,json_key: [(phoneNumber:chararray,firstName:chararray,lastName:chararray,email:chararray,alternatePickup:boolean,sendTextNotification:boolean,isSendTextNotification:boolean,isAlternatePickup:boolean)]);
it is not working!! Appreciate the help in advance.
JsonLoader allows loading only of correct json, while your format is actually CSV. There are three options for you ordered by incresing complexity:
Adjust your input format and make id part of it
Load data as CSV (as 2 fields: id and json, then use custom UDF to parse json field into a tuple)
Write custom loader that will allow you your original format.
You can use builtin JsonStorage and JsonLoader()
a = load 'a.json' using JsonLoader('a0:int,a1:{(a10:int,a11:chararray)},a2:(a20:double,a21:bytearray),a3:[chararray]');
In this example data is loaded without a schema; it assumes there is a .pig_schema (produced by JsonStorage) in the input directory.
a = load 'a.json' using JsonLoader();
I don't know the right way to store JSON object in a sqlite text field, using nodejs sqlite3 driver. I did the following:
Before write it to the table I tried JSON.stringify(jobj), now jstr value looks like {"melicode":9876543210}
But when I log jstr after select it:
console.log(jstr) # => { melicode :9876543210} no double quote inside
And, JSON.parse(jstr) fails with SyntaxError: Unexpected token m
Two things of note:
First, the result of your query is an Object, not a String. So attempting to parse it should result in an exception.
Second, Node's console.log does not render JavaScript objects in JSON notation. You can try that in the repl:
$ node
> console.log({"test": 123});
{ test: 123 }
i am working in extjs+yii. my server side design is in yii framework and client side design is in extjs. Now from extjs i am getting output data in json format as=
{"data":[{"optionId":"","questionId":"1","isAnswer":"","option":"Aus","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"2","isAnswer":"","option":"india","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"3","isAnswer":"","option":"England","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"4","isAnswer":"","option":"Srilanka","media":"","keyword":"","mediaTypeId":"","id":null}]}
So in Yii frameowk how to access this json fields? i want to access questionId and optionId fields of this json. So can someone please help me
You have to decode the json using CJSON::decode, and then access the fields:
$var='{"data":[{"optionId":"","questionId":"1","isAnswer":"","option":"Aus","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"2","isAnswer":"","option":"india","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"3","isAnswer":"","option":"England","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"4","isAnswer":"","option":"Srilanka","media":"","keyword":"","mediaTypeId":"","id":null}]}';
$decoded=CJSON::decode($var);
// now your json is stored in $decoded as an array, so you can access as follows:
echo $decoded["data"][0]["questionId"];
echo $decoded["data"][0]["optionId"];
// to loop over the elements use foreach
foreach ($decoded["data"] as $value){
echo $value["questionId"];
echo $value["optionId"];
}
Using CJSON::decode is better than relying on native json_decode, as when the native is not available CJSON::decode can still decode a json string. Also in some cases json_decode can return null for correct json strings, read here for a comparison of php json libraries.
How might you take JSON output (e.g., from http://www.kinggary.com/tools/todoist-export.php) and strip the names to yield just the values from each pair, as CSV or human-friendly text? Want a more readable, human-editable backup of my friend's data on todoist.com
Your example site generates XML for me, not JSON. In either case I'd probably reach for Ruby:
require 'net/http'
require 'rexml/document'
xml = Net::HTTP.get_response(URI.parse("http://www.kinggary.com/tools/todoist-export.php?completed=incomplete&retrieval=view&submit=Submit&process=true&key=MYKEY")).body
data = REXML::Document.new(xml)
data.elements.each('//task/content') do |e|
puts e.text
end
there's a good discussion of how to do this with Python at How can I convert JSON to CSV?
Can you JSON decode it to an array and just iterate the array for values? A sample of the JSON output would be helpful.
What language? PHP has a json_decode() function that turns the JSON into an object or associative array. You could then loop through the array or get the values from the object to turn it into whatever format you like.