Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • understanding json in perl- encoding and decoding. Why?

    Hello members,
    I am new to json. I know it is key value pair, and that is it.
    I am using language-Perl.

    What is encode, decode in it? Why do we require?
    Code:
    use strict;
    use autodie;
    use Data::Dumper;
    use IO::File;
    use warnings;
    use JSON::PP;
    my %rec_hash = ('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
    my $json = encode_json \%rec_hash;
    print "$json\n";
    
    my $son = '{
            "trans1": [20,30]
    }';
    
    
    print $son;
    my output is:

    {"e":5,"c":3,"a":1,"b":2,"d":4}
    {
    "trans1": [20,30]
    }
    Questions:
    1- when should I use hash for initiating json?
    2- I want to print output as

    {
    "trans1": [20,30]
    }
    This is the output I want in a new file. So is it a encoded one or decoded one? I will read this output from a python a script.

    Also, there is tonnes of info on utf-8 for Json, I couldn't get much on that.

    Please guide me here.
    Bioinformaticscally calm

  • #2
    JSON encoding will take a data structure (in Perl, Python, etc) and encode it as a string in a standard format. decode turns the string back into the data structure. You can encode literals (numbers, strings), arrays, and hashes.

    Whether you encode a hash or an array depends on the data you need to encode. In your example, you want to encode a hash to produce a JSON string that's wrapped in the "{ }" and has key-value pairs.

    Comment

    Latest Articles

    Collapse

    • seqadmin
      Essential Discoveries and Tools in Epitranscriptomics
      by seqadmin




      The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist...
      04-22-2024, 07:01 AM
    • seqadmin
      Current Approaches to Protein Sequencing
      by seqadmin


      Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
      04-04-2024, 04:25 PM

    ad_right_rmr

    Collapse

    News

    Collapse

    Topics Statistics Last Post
    Started by seqadmin, Yesterday, 08:47 AM
    0 responses
    12 views
    0 likes
    Last Post seqadmin  
    Started by seqadmin, 04-11-2024, 12:08 PM
    0 responses
    60 views
    0 likes
    Last Post seqadmin  
    Started by seqadmin, 04-10-2024, 10:19 PM
    0 responses
    60 views
    0 likes
    Last Post seqadmin  
    Started by seqadmin, 04-10-2024, 09:21 AM
    0 responses
    54 views
    0 likes
    Last Post seqadmin  
    Working...
    X