1

I have a json file like this :

  [{"id":"PMC102324",
"Original_paper":"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC102324/pdf",
"Annotated_file":"http://nactem10.mib.man.ac.uk/brat-v1.3/#/NCSTOX/PMC102324",
"Title":"Glucosinolate breakdown products as insect fumigants and their effect on carbon dioxide emission of insects",
"Molecule":[{"Main name":"glucosinolate", "Synonyms":[]},{"Mainame":"isothiocyanate", "Synonyms":[]},{"Main name":"hexane", "Synonyms":    []},{"Main name":"sinigrin", "Synonyms":[]},{"Main name":"allyl glucosinolate", "Synonyms":[]},{"Main name":"rotenone", "Synonyms":[]},{"Main name":"sucrose", "Synonyms":[]},{"Main name":"thiocyanate", "Synonyms":[]},{"Main name":"allyl isothiocyanate", "Synonyms":[]}],
"ToxKeywords":"safety, cytotoxic, ",
"Important_sentences":["The mode of action of many isothiocyanate compounds has also been attributed to their capability for alkylating the nucleophilic groups of biopolymers such as DNA, thus having cytotoxic properties which can affect the formation of the spiracular epidermis and crochet on the prolegs of tobacco hornworm (Manduca sexta L.) caterpillars [28-30]."]
}]

When i use json_decode the var_dump() return :

 array (size=1)
 0 => 
array (size=7)
  'id' => string 'PMC102324' (length=9)
  'Original_paper' => string 'https://www.ncbi.nlm.nih.gov/pmc/articles/PMC102324/pdf' (length=55)
  'Annotated_file' => string 'http://nactem10.mib.man.ac.uk/brat-v1.3/#/NCSTOX/PMC102324' (length=58)
  'Title' => string 'Glucosinolate breakdown products as insect fumigants and their effect on carbon dioxide emission of insects' (length=107)
  'Molecule' => 
    array (size=9)
      0 => 
        array (size=2)
          ...
      1 => 
        array (size=2)
          ...
      2 => 
        array (size=2)
          ...
      3 => 
        array (size=2)
          ...
      4 => 
        array (size=2)
          ...
      5 => 
        array (size=2)
          ...
      6 => 
        array (size=2)
          ...
      7 => 
        array (size=2)
          ...
      8 => 
        array (size=2)
          ...
  'ToxKeywords' => string 'safety, cytotoxic, ' (length=19)
  'Important_sentences' => 
    array (size=1)
      0 => string 'The mode of action of many isothiocyanate compounds has also been attributed to their capability for alkylating the nucleophilic groups of biopolymers such as DNA, thus having cytotoxic properties which can affect the formation of the spiracular epidermis and crochet on the prolegs of tobacco hornworm (Manduca sexta L.) caterpillars [28-30].' (length=343)

My goal is to get what's in the 'Molecule' => array

Contrôler :

 /**
 * @Route("/parse_file", name="parseFile")
 * @method("GET")
 */
public function parseFile()
{
    $em = $this->getDoctrine()->getManager();
    $em->getRepository('NcstoxBundle:JsonTextMining');

    set_include_path('/home/landreau/workspace/NCSTOX/web/assets/json/sample-json');
    $json = file_get_contents('PMC102324.json', FILE_USE_INCLUDE_PATH );
    $tab = json_decode($json, true);

    var_dump($json);
    var_dump($tab);

    foreach ($tab as $item) {
        $jsonTextMining = new JsonTextMining();
        $jsonTextMining->setSolrId($item['id']);
        $jsonTextMining->setOriginalPaper($item['Original_paper']);
        $jsonTextMining->setAnnotatedFile($item['Annotated_file'][0]);
        $jsonTextMining->setTitle($item['Title'][0]);
        $jsonTextMining->setMolecule($item['Molecule']['Main name']);
        $jsonTextMining->setMolecule($item['Molecule']['Synonyms']);
        $jsonTextMining->setKeyword($item['ToxKeywords'][0]);
        $jsonTextMining->setImportantSentence($item['Important_sentences'][0]);


        $em = $this->getDoctrine()->getManager();
        $em->persist($jsonTextMining);
    }

    $em->flush();


    return new Response('Saved new document with id ' . $jsonTextMining->getSolrId());
}

It works with all $items exept for Molecule, i tried :

            $jsonTextMining->setMolecule($item['Molecule']['Main name'][0]);

and others ways do you think there is a way to get what's in this json array or should i reformat the json ?

6
  • Molecule itself is an array so you need to further loop through this or do $item['Molecule'][0]['Synonyms']? Commented Jun 15, 2017 at 14:13
  • I think we get closer but i got this error now : Type error: Argument 1 passed to NcstoxBundle\Entity\JsonTextMining::setMolecule() must be an instance of Doctrine\Common\Collections\ArrayCollection, string given, called in /home/landreau/workspace/NCSTOX/src/NcstoxBundle/Controller/DefaultController.php on line 128 Commented Jun 15, 2017 at 14:18
  • json_decode($json, true)[0]['Molecule'][0]['Main name'] Commented Jun 15, 2017 at 14:19
  • The answer really depends on what $jsonTextMining->setMolecule is expecting!!! Does it want (x) main names? Or an array of Molecule names ?? OR WHAT Commented Jun 15, 2017 at 14:23
  • i got this error perhap's will answer your question : Type error: Argument 1 passed to NcstoxBundle\Entity\JsonTextMining::setMolecule() must be an instance of Doctrine\Common\Collections\ArrayCollection, string given, called in /home/landreau/workspace/NCSTOX/src/NcstoxBundle/Controller/DefaultController.php on line 128 Commented Jun 15, 2017 at 14:31

3 Answers 3

1

try this:

$result = json_decode($json, true);

If you use true as second parameter, json_decode create an associative array

For Molecule you can make this:

$item['Molecule'][0]['Main name']

or this:

$item[0]['Molecule'][0]['Main name']

You have many Molecule so I think you need many insert

Sign up to request clarification or add additional context in comments.

2 Comments

The result is what's in my post when i var_dump $tab
@AlessandroMinoccheri Try echo $item[0]['Molecule'][0]['Main name']
1

try this decode the json with json_decode($json, true) for array :

<?php
    $json = '[{"id":"PMC102324",
    "Original_paper":"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC102324/pdf",
    "Annotated_file":"http://nactem10.mib.man.ac.uk/brat-v1.3/#/NCSTOX/PMC102324",
    "Title":"Glucosinolate breakdown products as insect fumigants and their effect on carbon dioxide emission of insects",
    "Molecule":[{"Main name":"glucosinolate", "Synonyms":[]},{"Mainame":"isothiocyanate", "Synonyms":[]},{"Main name":"hexane", "Synonyms":    []},{"Main name":"sinigrin", "Synonyms":[]},{"Main name":"allyl glucosinolate", "Synonyms":[]},{"Main name":"rotenone", "Synonyms":[]},{"Main name":"sucrose", "Synonyms":[]},{"Main name":"thiocyanate", "Synonyms":[]},{"Main name":"allyl isothiocyanate", "Synonyms":[]}],
    "ToxKeywords":"safety, cytotoxic, ",
    "Important_sentences":["The mode of action of many isothiocyanate compounds has also been attributed to their capability for alkylating the nucleophilic groups of biopolymers such as DNA, thus having cytotoxic properties which can affect the formation of the spiracular epidermis and crochet on the prolegs of tobacco hornworm (Manduca sexta L.) caterpillars [28-30]."]
    }]';
    $array = json_decode($json, true);
    echo "<pre>";
    print_r($array[0]["Molecule"]);

3 Comments

@Gy0m Then something is not as you are telling us
@Gy0m if you just run this code i am sure you will not get any error, i think your case is different please update your full code
The full code is already here it's just that i am into symfony perhap's he's got different syntax
1

I can't comment because of my reputation (If I could, I would because Alessandro Minoccheri answer seems like the good one.)

I did what Alessandro did and it worked for me :

$res = json_decode($json, true);
print_r($res[0]['Molecule']);

Give me that :

Array
(
    [0] => Array
        (
            [Main name] => glucosinolate
            [Synonyms] => Array
                (
                )

        )

    [1] => Array
        (
            [Mainame] => isothiocyanate
            [Synonyms] => Array
                (
                )

        )
...

Isn't it what you wanted ? If you want each Mainname, I think you will need to do a foreach.

4 Comments

When i put [0] before the tag it gives the error Notice: Undefined offset: 0 I'm into Symfony
Have you tried using print_r($res) ? Sometimes var_dump is too abstract and you don't see why it could be wrong. I worked a little with symfony and this syntax worked. I cannot see why it wouldn't work expect if something really obvious is there and we don't see it.
print_r show me that the arrays are not empty : Array ( [0] => Array ( [id] => PMC102324 [Original_paper] => ncbi.nlm.nih.gov/pmc/articles/PMC102324/pdf [Annotated_file] => nactem10.mib.man.ac.uk/brat-v1.3/#/NCSTOX/PMC102324 [Title] => Glucosinolate breakdown products as insect fumigants and their effect on carbon dioxide emission of insects [Molecule] => Array ( [0] => Array ( [Main name] => glucosinolate [Synonyms] => Array ( ) ) [1] =>
Well, sorry, I can't find an answer to that. While researching, then only thing I saw is an error in the JSON... Mainame instead of Main name for the 2nd Molecule. Except that, I couldn't reproduce your issue and couldn't find the cause, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.